A WaitGroup is a powerful synchronization primitive in Go that allows you to wait for a collection of goroutines to finish executing. The pattern is simple: call Add() before launching each goroutine, call Done() when a goroutine completes (typically using defer), and call Wait() to block until all goroutines are done.
This is one of the most common ways to coordinate goroutines without using channels. It’s particularly useful when you don’t need to exchange data between goroutines but simply need to know when they’ve all finished their work. The defer wg.Done() pattern ensures that Done() is called even if the goroutine panics.
WaitGroups are safe for concurrent use and can be reused after Wait() returns, making them flexible for complex goroutine orchestration patterns.