sync.WaitGroup makes one goroutine wait until a group of other goroutines has finished. Go has no built-in semaphore type, so you limit concurrency with a buffered channel, or with golang.org/x/sync/semaphore or errgroup.Group.SetLimit when you need weighted or error-aware limits. The two tools solve different problems: a wait group answers "are they all done?", while a semaphore answers "how many may run at once?".
Used together, they let you start many goroutines, cap how many run in parallel, and wait for the rest to finish before continuing.
sync.WaitGroup
sync.WaitGroupA WaitGroup keeps an internal counter. Add(n) raises it, Done() lowers it by one, and Wait() blocks until it reaches zero.
The order of the worker lines varies from run to run, but the last line always prints last. Three rules prevent most bugs:
Add before the go statement, never inside the goroutine, or Wait may return too early.Done with defer, so panics and early returns still decrement the counter.WaitGroup by pointer. Copying it breaks the counter, and go vet reports that mistake.A negative counter causes a panic, and a WaitGroup can be reused only after all previous Wait calls have returned.
WaitGroup.Go in Go 1.25 and laterGo 1.25 added a Go method that does the Add and Done bookkeeping for you:
This also works because, since Go 1.22, each loop iteration has its own i. On older toolchains use the classic Add/Done form.
A semaphore is a counter that permits a fixed number of holders. The idiomatic Go version is a buffered channel: sending acquires a slot, receiving releases it.
At most three jobs print and sleep at the same time. struct{} is used because it takes no memory. This pattern works for rate-limiting database connections, outbound HTTP calls or file handles.
Starting 10 goroutines that mostly wait is fine, but for very large inputs it is better to limit the number of goroutines too, using the worker pool below.
A fixed number of workers reading from a channel gives the same cap without creating a goroutine per task:
Closing the channel ends each worker's range loop, then Wait returns.
When tasks need different amounts of a resource, or you want cancellation, use the golang.org/x/sync module:
errgroup combines a wait group, a concurrency limit, error propagation and context cancellation. For weighted limits, semaphore.NewWeighted(n) provides Acquire(ctx, weight) and Release(weight). Install it with go get golang.org/x/sync.
| Need | Use |
|---|---|
| Wait for several goroutines | sync.WaitGroup |
| Cap parallel work, simple | Buffered channel |
| Cap work with errors and cancellation | errgroup with SetLimit |
| Different cost per task | semaphore.Weighted |
| Protect shared data | sync.Mutex |
No. The standard library has no semaphore type. Use a buffered channel, or golang.org/x/sync/semaphore for weighted semaphores.
Add be called before starting the goroutine?If Add runs inside the goroutine, Wait might execute first, see a zero counter and return before the work has begun.
sync.WaitGroup?No. Copying duplicates the counter and the goroutines then wait on different copies. Pass a pointer, and let go vet catch mistakes.
If Wait never returns because Done is missed, the program hangs. The Go runtime panics with "all goroutines are asleep" only when every goroutine is blocked. Use defer wg.Done() and run tests with the -race flag and timeouts.
Use sync.WaitGroup to wait for goroutines, call Add before launching them, and defer Done. Use a buffered channel, a worker pool or errgroup.SetLimit to cap concurrency, and reach for semaphore.Weighted when tasks have different weights. Go 1.25 and later also offer WaitGroup.Go for less boilerplate.