Go Scatter Gather Pattern

One of the strengths of Go is it’s native support for concurrency. There are a lot of posts about concurrency in Go, but I was writing this up as a simple example for a coworker and decided to publish it.

It takes a little getting used to if you don’t use concurrent programming a lot, but once you get beyond a couple hurdles, it’s a rockin’ good time. In addition, you can eliminate a whole class of bugs when you force all communication through channels. Then again, you open up another kind of bug. Be careful though, it’s easy to get carried away with channels and then you can end up with some very difficult to read new age spaghetti code.

I think the term scatter/gather is an old one, but it corresponds to the fan-out/fan-in pattern in Go. I think it’s elegant and works very well.

Contrived scatter/gather example in Go

You can play with this code on the Go Playground. For more about Go concurrency patterns check out pipelines or stay tuned! I am going to write up a few more because I have been asked by a few people about simple examples.

Written by

Technologist and taco lover.

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