Menu

vInsert.nl

What am I even on about

Golang WaitGroup

WaitGroup is fairly simple to understand and use, since it’s just a counter that stops blocking when it hits zero. You add to the counter before you create a goroutine, and subtract from the counter once the goroutine is done. You can utilize this to make a thread ‘wait’ for other concurrent operations to finish.

This is much more semantic and flexible than messing around with ready-ness using channels, since the latter will result in a mess if you don’t know how much goroutines you’re creating ahead of time. You can also keep incrementing the counter by simply Adding to the WaitGroup some more! For example:

Thanks for reading!

Leave a Reply