

package main
import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
If you run this, try pointing your browser at http://localhost:8080/Dice. You'll get a page that says:
Hi There, I love Dice!
What's So Special About Go?
It's been designed to compile fast, i.e., turn the human readable code into special instructions that the computer understands. A large Go program can be compiled in just a few seconds instead of minutes or even hours that some other languages take. Part of this has been done by avoiding C-like include files, which in some cases can pull in thousands of lines of code that mostly aren't even needed. That slows compilation down a lot. Go doesn't need makefiles or any kind of build configs to help turn a set of project files into a working program. It figures out dependencies from the source files themselves. All it needs to know is which folders contain source files, and that's done by setting the environment variable GOPATH. Hardly any semi-colons are used, which improves readability. The compiler treats them as if they were there at end of lines or between statements when it's appropriate. This shortens the code and makes it cleaner and easier to read. A major strength is the sophisticated and yet easy to code multi-threading based on processes sending messages to each other, called Communicating Sequential Processes. Functions define a channel type, which in turn defines the type of data channels send to each other. With computers having multi-core Central Processing Units (CPUs) this simplifies enormously the task of writing code. Running a single threaded program on a multi-core is like driving a car that's only firing on one cylinder. To give you a better feel, here's an example of a Prime Number sieve using channels that output prime numbers. These are numbers that start at 2 and can only be divided by themselves or 1 and leave no remainder. A sieve works by removing all numbers (after 2) that can be divided by 2, then removing from those that are left that can be divided by 3, then 5, etc. After you've removed all those you are left with prime numbers. It also demonstrates variable declaration, for loops and function definition and calls.package main import "fmt" // Send the sequence 2, 3, 4, _ to channel 'ch'. func generate(ch chan<- int) { for i := 2; ; i++ { ch <- i // Send 'i' to channel 'ch'. } } // Copy the values from channel 'src' to channel 'dst', // removing those divisible by 'prime'. func filter(src <-chan int, dst chan<- int, prime int) { for i := range src { // Loop over values received from 'src'. if i%prime != 0 { dst <- i // Send 'i' to channel 'dst'. } } } // The prime sieve: Daisy-chain filter processes together. func sieve() { ch := make(chan int) // Create a new channel. go generate(ch) // Start generate() as a subprocess. for { prime := <-ch fmt.Print(prime, "n") ch1 := make(chan int) go filter(ch, ch1, prime) ch = ch1 } } func main() { sieve() }The sieve function make( ) allocates and initializes an instance of a channel that transmits ints. The statement go generate(ch) starts the generate function running (in a parallel process) outputting to this channel. The declaration func generate(ch chan <- int) tells it that it's sending ints to channel ch. So where can you see Go in action? One place is Google's Thanksgiving doodle for 2011, which lets you change feathers and legs in up to 800 million combinations. The engineer involved had never programmed in Go before and had two days to learn it and produce production ready code! You can view the source code on this Google Code page and read about his efforts.