Why You Should Learn Go

by redshift

I’m pretty excited about Go, the new programming language from Google. It’s been my obsession over the last few weeks, and I think if you’re a programmer, you should learn it.

Why did I start? Out of respect for its creators, Rob Pike and Ken Thompson. They helped create and popularize Unix, C, UTF-8, Plan 9, and many other things. It may seem like they’ve been under the radar lately in their research wing at Google, but I think they’ve just been building up a foundation of code and ideas.

Why is it exciting? It seems to have solutions for concurrency, compilation speed, and safety, while still looking somewhat familiar. You can run hundreds of thousands of concurrent goroutines. The entire Go library compiles in ~5 seconds. Go code runs almost as fast as C, without even being optimized yet. It’s type-safe without being too verbose.

It’s not as high level as Ruby or Python, but it can be more mentally satisfying in an OCD geek kind of way. It’s C without the pain.

Sure, it took ideas from Alef, Limbo, Newsqueak, and other languages (watch this then this for proof, and for enlightenment) – but how else can you make progress? Go has the momentum those languages never got.

And I, for one, am glad that the legacy of Bell Labs lives on.


Here’s a code sample that I think particularly illustrates the elegance of the language, taken from the Go tutorial. Whether you come from an imperative or declarative background, I think it should be impressive.

This generates prime numbers with the standard sieve of Eratosthenes, but it does it by creating a steam of integers and piping together filters for each prime to exclude multiples. It’s all run concurrently and uses synchronous channels for communication. And it’s fast.

func generate() chan int {
  ch := make(chan int)
  go func(){
    for i := 2; ; i++ {
      ch <- i
    }
  }()
  return ch
}

func filter(in chan int, prime int) chan int { out := make(chan int) go func() { for { if i := <-in; i % prime != 0 { out <- i } } }() return out }
func sieve() chan int { out := make(chan int) go func() { ch := generate() for { prime := <-ch out <- prime ch = filter(ch, prime) } }() return out }
func main() { primes := sieve() for { fmt.Println(<-primes) } }

If you find Go interesting, you can get involved by writing some open-source libraries. The standard library has a lot to go on, and cat-v has a list of resources to check out.