Go synchronises goroutines with channels and with the sync and sync/atomic packages. Use channels to pass data and signal events between goroutines, sync.Mutex or sync.RWMutex to protect shared state, sync.WaitGroup to wait for a group of goroutines, sync/atomic for simple counters and flags, and context.Context to cancel work. The Go proverb is "do not communicate by sharing memory; share memory by communicating", but a mutex is often the simpler right answer.
This guide explains each tool with a runnable example, how to choose between them, and how to find mistakes with the race detector. Examples target Go 1.24 and later.
A channel carries values between goroutines. A send and the matching receive are synchronised: everything the sender did before the send is visible to the receiver after the receive. An unbuffered channel makes sender and receiver meet; a buffered channel lets the sender run ahead until the buffer is full.
Rules worth remembering: only the sending side should close a channel, sending on a closed channel panics, receiving from a closed channel returns the zero value immediately, and a nil channel blocks forever. Use select to wait on several channels, add a timeout or ctx.Done() case, and use the <-chan T and chan<- T direction types to document intent.
When several goroutines read and write the same variable, protect it with a sync.Mutex. Keep the critical section short and pair Lock with a deferred Unlock.
sync.RWMutex allows many concurrent readers (RLock) or one writer (Lock). It pays off when reads greatly outnumber writes and the critical section is not tiny; otherwise a plain Mutex is often faster. Never copy a mutex after first use, and never lock it again from the same goroutine, because Go mutexes are not reentrant.
sync.WaitGroup waits until a set of goroutines finishes. Call Add before starting the goroutine, and Done when it ends. Go 1.25 added wg.Go(f), which does the add, the goroutine start and the deferred Done in one call:
On earlier versions, use wg.Add(1) and defer wg.Done() as in the previous example. When goroutines can fail and you want the first error plus cancellation, use golang.org/x/sync/errgroup instead.
For single variables such as counters and flags, sync/atomic avoids a lock. Since Go 1.19 the typed values (atomic.Int64, atomic.Bool, atomic.Pointer[T]) are preferred over the older atomic.AddInt64 functions because they are harder to misuse.
Atomics only protect one value at a time. If two fields must change together, use a mutex.
| Tool | Use it for |
|---|---|
sync.Once and sync.OnceFunc, OnceValue | One-time initialisation that is safe from many goroutines |
sync.Cond | Waiting for a condition on shared state (rare; channels are usually simpler) |
sync.Map | Caches with stable keys or write-once, read-many data |
sync.Pool | Reusing temporary objects to reduce allocation |
context.Context | Cancellation and deadlines across goroutines |
A data race happens when two goroutines access the same memory, at least one writes, and nothing orders them. Races cause corrupt data that appears only occasionally. Go ships a race detector:
It reports the conflicting accesses with stack traces and should run in CI. Also watch for deadlocks (the runtime panics with "all goroutines are asleep" when every goroutine blocks) and goroutine leaks from channel operations nobody completes. Always give blocking operations a way out through select and a context.
They are synchronised: a send happens before the matching receive completes, so there is no race on the value passed. That does not make the surrounding logic atomic, so shared data still needs coordination.
When reads are much more frequent than writes and each read holds the lock for a meaningful time. Benchmark both, since RWMutex has more overhead.
No. Close a channel only when receivers need to know that no more values will come, for example to end a range loop. Unclosed channels are garbage collected.
Pass a context.Context, select on ctx.Done() in every loop that blocks, and wait for exit with a WaitGroup or errgroup.
Go gives you channels for passing data and signalling, mutexes for protecting shared state, WaitGroup and errgroup for waiting, atomics for single values and context for cancellation. Choose the simplest tool for the ownership pattern, keep critical sections small and run go test -race to catch the mistakes that slip through.