I have been hearing about Go a lot lately including it being the most requested language at my work. This got me interested in why people find it so appealing. Developer enthusiasm boils down to one of a few reasons:
- The New Hotness
- Marketing (Develop 50% faster, that’s more time for showers!)
- Developers wanting to learn something new
Let’s do some examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
This example takes a url and makes a network request within a go routine. The function immediately returns a channel while the network request is being accessed in the background. The caller code looks like this.
1 2 3 4 5 6 7 8 9 10 11 12 13
This code gets a reference to the Go channel and blocks waiting for any messages on the channel. This is a trivial example, but shows you how things happen asynchronously and it’s very easy to block where needed for concurrency. In this case, we block by attempting an assignment from the channel returned from utils.Get.