Learning Go
Go is an interesting language, and it seems to be gaining significant traction as a systems programming language, especially in the web and distributed systems domains.
Here's a quick list of some of my favorite features:
- Statically typed
- Compiled
- First-class functions
- Composition over inheritance
- Concurrency is part of the language
- Package system that acknowledges the Internet
It's certainly an opinionated language, but if your opinions align, I think you will find it to be an enjoyable language.
Concurrency is a very important topic today, if you want to take full advantage of modern hardware you need to have multiple threads execute in parallel on more than one CPU core. (Whether the programmer has to manually manage those threads or not is a separate question, in the end there needs to be some mechanism in the language to start concurrent execution.)
To give you a taste of concurrency in Go, here's a short example.
Here is how you might write a function to sum a list of integers. To gain some concurrency, you could break the list in half and concurrently sum the halves.
Here, I create a short wrapper that accepts a list of int and a channel. Channels are essentially thread-safe FIFO queues. In concurrentSum
, a channel of int
is created. This will be used by the workers to report back partial sums for each half. I use the defer
keyword to execute the given statement when this scope exits. It is a very convenient way to deal with cleanup code that you might put in finally
blocks in other languages.
The real magic happens with the go
keyword. This will execute the given statement concurrently. Here, I use it to start the two workers that will sum each half of the list. In the final line, I read from the channel twice, and sum the two partials to produce the final value.
This is a pretty dense function, and it shows how easily the concurrency primitives can be combined together to do something that would involve more bug prone boiler-plate in other languages. Concurrency is important, but as a programmer you should not have to suffer through low level locking and visibility issues just to get something basic accomplished. With Go, you get nice primitives that hides these concerns with abstraction. (Don't worry, locks and other basic primitives are there, if and when you need them.)
I think there are some powerful concepts here and I'm excited to try them out on a larger project. It seems to be a fantastic mix of high and low level approaches, I can certainly see why Go is gaining traction.