This 35-minute video from the recent Google I/O conference explains how to use Go’s concurrency primitives — goroutines, channels, and the
select statement — to do things elegantly, correctly, and safely in a few lines of Go, which would otherwise turn your brain into a pretzel in most programming languages.
My favorite thing about Go is that a good Go program looks self-evident and obvious, even when it may be doing things that would be insanely complex in another language. Callbacks, closures, mutexes, and so on just disappear, and the program itself emerges, looking completely unimpressive. In many cases I think “what’s the big deal about that?” until I realize how hard it would be to write in Java, or Perl, or so on. A lot of the code in Percona Toolkit, for example, involved “pipelines” of callbacks passing data along to other callbacks for further processing. These were hard to reason about, hard to make resilient to errors and allow clean termination, and were redesigned several times, never very successfully in my opinion. In Go, channels make those kinds of tasks so simple. Such a program in Go looks suspiciously like a Unix | pipe | and | filter | program. If you think about it, the Unix shell itself is a great example of using “channels” successfully to trivialize what would otherwise be a migraine-inducing task.