Go's standard library implements concurrency with goroutines, channels, the sync and sync/atomic packages, and context for cancellation. Goroutines run work concurrently, channels pass data between them, sync.WaitGroup and sync.Mutex coordinate them, and context stops them cleanly. Most patterns, such as worker pools, pipelines and fan-out/fan-in, are built from these pieces.
The original examples on this topic used time.Sleep to wait for goroutines, which is unreliable. The versions below synchronise properly.
sync.WaitGroupcontextsync.WaitGroupA goroutine starts with the go keyword. The program exits when main returns, so you must wait for the work instead of sleeping.
Add registers one unit of work, Done marks it complete and Wait blocks until the counter reaches zero. Since Go 1.22 each loop iteration has its own i, so the closure captures the right value. In Go 1.25 and later you can also write wg.Go(func() { ... }), which combines Add, the goroutine start and Done. Output order is not guaranteed.
Channels send values between goroutines. An unbuffered channel synchronises sender and receiver. A buffered channel lets the sender run ahead up to its capacity.
The producer owns the channel and closes it when done, which ends the range loop. The return type <-chan int is receive-only, so callers cannot send on or close it. The rule is that only the sender closes a channel, and never close it twice.
A worker pool limits concurrency to a fixed number of goroutines that read jobs from a shared channel.
The pool size caps how many jobs run at once. Closing jobs tells workers to finish, and a separate goroutine closes results after every worker is done so the final range terminates.
contextGoroutines that can block must be stoppable. Pass a context.Context and watch ctx.Done().
Without the ctx.Done() case the producer would block forever once nobody reads, which is a goroutine leak. For running several tasks and collecting the first error, golang.org/x/sync/errgroup is the common extension, though it is outside the standard library.
When goroutines share memory, guard it. Use sync.Mutex for compound updates, sync/atomic for simple counters and sync.Once for one-time initialisation.
Maps are not safe for concurrent writes, so the mutex protects m. Run tests with go test -race to catch data races. Pick the tool by need: channels to hand over ownership of data, mutexes to protect state that many goroutines touch.
| Pattern | Use when | Building blocks |
|---|---|---|
| Fire and wait | A fixed set of independent tasks | go, sync.WaitGroup |
| Pipeline | Stages transform a stream | Channels, one goroutine per stage |
| Worker pool | Limit parallelism | Job channel, N workers |
| Fan-out / fan-in | Spread work, merge results | Several workers, one results channel |
| Timeout and cancel | Calls that can hang | context, select |
| Shared counter or cache | Many goroutines update state | sync.Mutex, sync/atomic, sync.RWMutex |
Use sync.WaitGroup, or a channel that the goroutine closes or sends on. Do not rely on time.Sleep.
Use unbuffered when you want sender and receiver to synchronise. Use buffered to absorb bursts or set a concurrency limit, and size the buffer deliberately.
A goroutine that blocks forever, usually on a channel nobody reads or writes. Prevent it with context cancellation and by always giving senders a way out.
Run go run -race or go test -race. The race detector reports unsynchronised access to shared memory.
Go's concurrency toolkit is small: goroutines, channels, sync, sync/atomic and context. Wait with WaitGroup, let the sender close channels, bound parallelism with worker pools, cancel with context and protect shared data with mutexes or atomics. Verify everything with the race detector.