Web development using Go

Go has enjoyed unusually rapid adoption and popularity among developers. Here are some highlights of using this programming language.

The Go programming language is one of the newer languages available, only released for public consumption in November 2009 and christened as production-worthy by Rob "Commander" Pike as of May 2010. Nevertheless, Go has enjoyed unusually rapid adoption and popularity, which might be due in part to the backing of Google, the fame of its authors (yes, that Ken Thompson), or its innate coolness.

Go has concurrency baked into the language in the form of goroutines, which are conceptually similar to threads, but the language manages their mapping to real threads (or not) under the hood. The syntax for creating goroutines is remarkably simple, and Go provides a special data type called channels to facilitate communication between goroutines (and between goroutines and the main thread). A web server provides a natural application for this kind of parallelism: a separate goroutine can handle each web request.

Go is designed to be a system language, like C. Its syntax is similar to C's, but with numerous relaxations and improvements. Its most beneficial similarity to C, though, is its performance. Go is a compiled language, so you can bet that it will scale up to increased traffic better than interpreted alternatives like Ruby, Python, and PHP. On the other hand, that means you have to recompile and link components of your web server whenever you change the code. It's a trade-off.

Like C, Go has pointers -- but in Go a pointer is much less likely to become an anti-foot cannon. Go provides garbage-collection instead of relying on the programmer or a class implementation like smart pointers to free them. Go prohibits casting and pointer arithmetic.

Even though Go provides strong, static type safety, it also allows for more dynamic typing via interfaces. Superficially, Go interfaces are similar to Java interfaces in that both specify a minimum set of methods that an implementer of an interface must provide -- but Go's implementation is much more fluid. For instance, you can declare a parameter as an interface with no methods -- which allows you to pass anything. Any type that implements the methods of an interface implicitly implements the interface, without having to say that it does. Thus, Go interfaces are more like duck-typing laundry lists. Unlike duck-typing in late-binding languages like Python and Ruby, however, the implementation requirements in Go get checked by the compiler.

Go already has an active and helpful community. On the golang-nuts Google group you're likely to get a prompt answer from people deeply involved in the project, like Russ Cox or Commander Pike himself.

When it comes to creating your web server, you have at least three options: (1) the http package that comes with the language, (2) Web.Go (inspired by web.py for python), and (3) Twister (inspired by Tornado, which was itself at least partially inspired by web.py). All three of these promote the approach of registering handlers to match incoming URIs and then calling a Server.Run method to dispatch requests. Web.Go provides explicit support for shared hosting via SCGI or FastCGI.

For templating, Go provides its own template package. There's also a version of the popular mustache logic-less template engine available. Twister includes examples of using the template package with the templates organized as HTML pages much like eRuby or PHP. Web.Go recommends using mustache instead, because it can do recursive templating.

What's a web application without a database? Work is progressing on Go bindings for many popular databases, including MySQL, PostgreSQL, SQLite3, and even ODBC (if you're so inclined). As of this writing, the SQLite3 bindings appear to be the most mature of the bunch, especially Eleanor McHugh's fork.

When would you want to use Go for your site? The Go language and supporting packages sre still in a state of rapid development, so unless you're in an experimental frame of mind you might want to avoid sinking too much effort into work that might become obsolete soon. On the other hand, it looks like Go hits the sweet spots between performant parallelism and ease of programming, dynamic and safe typing, simplicity and a multitude of options. You might find it useful sooner than you think.