This was originally posted on LinkedIn on November 2016

It’s very ironic that Go was “designed with concurrency in mind”, but at language level its concurrency support is very poor.

Let’s start with simplest things: data structures. You would expect that a truly concurrent language will provide you concurrent data structures. But it doesn't. And language designers are even proud of that, it seems.

Some really simple example. We’ll have 10 goroutines put 1000 random numbers each in a map, then check its size. Based on internal implementation, this may display 10,000 or less. Let’s see what happens:

So, instead of corrupting non-concurrent data structure Go decides the crash the entire thread. I begin to wonder which is worse.

OK, manual work incoming!

We got there, eventually. But with little help from the language itself.

Of course this could be solved with channels. After all, one of the most basic Go proverbs is “share memory by communicating”. But there’s also a caveat to channels. They are always bounded. And if you reach the bound? You’re blocked. Not very concurrent, right?

Another point is that Go runtime won’t wait for goroutines to finish. Unlike Executor in Java, for example:

This will result in “Finished” printed 10 times. Duh.

Curiously for some, same in Go won’t print anything:

That’s because main goroutine doesn’t wait for child goroutines to complete.

But there’s WaitGroup, similar to CountDownLatch in Java. That should solve the problem, right?

Ouch! It seems that for the most basic concurrency case you also need to remember to use pointers. Yeah, those strange symbols that some of us hoped would die out with C++.

Luckily, for the simplest cases like this one, using closure works too:

So, albeit concurrency in Go works, and does so pretty well, it’s not simpler than other modern languages by any means, as some state. And sometimes, it seems, even more complex. There’s even a book by O’Reilly on the matter. Sounds like fun, right?

Here’s also the link to the article (now classic) that inspired this post: 50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs

Solutions Architect @Depop, author of “Hands-on Design Patterns with Kotlin” book and “Web Development with Kotlin” course

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store