Go For It

Published on 27 May 2010
This post thumbnail

I've been exploring new concurrent programming approaches lately, and decided to give Google's new Go programming language a try. It's fast, straightforward, and a good fit wherever you might use C or C++,  such as systems programming. It borrows a lot from C (including syntax, pointers, and the gcc compiler), but adds significant improvements, including garbage collection and its own flavor of coroutines called goroutines. "Goroutines" is certainly clever, but I'll never understand why Google gave its creation the decidedly ungoogleable name of "go" (hence the alternate name, golang: so you can find it).

To get started, I had to check out sources from mercurial and build them, but the instructions were clear and complete ("cookbook", as we say). They were written for Linux (complete with _sudo apt-get install_s for gcc, bison, and other pre-reqs), so I used Ubuntu. I suppose it's possible to develop under Windows with Cygwin, but I didn't try it.

The emphasis on clean, lightweight, and terse code is clear throughout. For example, it takes a simple approach to common OO mainstays: it uses structs, types, and interfaces instead of classes and inheritance (like C#, structs can have methods); new and make allocators with automatic reference counting; and a built-in map syntax instead of dictionary or hash table classes. It provides syntactic sugar for common shorthands, such as := to declare and initialize a variable in one step, permission to omit semicolons and parentheses, and code-reducing conveniences like returning multiple values from a function. It uses Java-style packages and imports for organizing code and supports Python-inspired slices for array references.

Goroutines are surprisingly easy to use. Any function call can be invoked as a goroutine (forked) simply by prefixing the call with the keyword go. The function itself can be inner, like a block or anonymous function. Channels provide the communication and rendezvous vehicle (like a future and queue) via a unique and simple syntax.

There's a lot missing from the language (much of that deliberate, like classes and inheritance), and it lacks maturity and production readiness. But it's easy to appreciate how its primary inventions (goroutines, channels, maps, slices, return lists, and implied interfaces) work together to form a clean, cohesive toolbox for fast concurrent programming. While I don't foresee using go for "real work", I hope to see it influence other environments which often make parallel programming harder than it has to be.