Go Scatter Gather Pattern

Guy J Grigsby
Star Gazers
Published in
1 min readOct 6, 2020

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.

--

--

Guy J Grigsby
Star Gazers

Technologist, taco lover and Gopher. Technologist and software engineer in distributed systems. Neuro-divergent.