Google Go and How It Works

Google GophersGoogle Go is a systems programming language (just over two years old) invented at Google and which can best be summed up as the new C: It’s simple, easy to learn, and very fast to compile. (The Gopher, by the way, is the language’s mascot).

Is Go a language worth learning, or just another flavor of the month? Share your opinion in the comments below.

The first release includes the language and a set of core libraries. If you have Linux, FreeBSD, Mac OS X or Windows, then you can download and learn how to use it. Go is totally free (open source, released under a liberal BSD license) so anyone can download it from the Go website and start learning through its tutorials and excellent tour.

Here’s what it looks like. This is a full working example of a simple Web server:

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.

Conclusion

With any new programming language, the big question is, “will it last?” Go has already built up a solid community around it, and the release will certainly strengthen it. Google’s started using it for some of its software, and other teams have as well. Take a look at the large number of projects. They indicate a very strong level of interest.

The second question is can this be used for online game development, particularly servers? The answer is a most definite yes. So, I’ll be returning to this in the future.

Post a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>