Go protects shared data between goroutines with the sync and sync/atomic packages. Use sync.Mutex or sync.RWMutex to guard multi-step updates, sync.WaitGroup to wait for goroutines to finish, sync.Once for one-time initialisation, and the typed values in sync/atomic (such as atomic.Int64) for single counters and flags. Run tests with go test -race to catch the mistakes you miss.
Without synchronisation, two goroutines that touch the same variable with at least one write form a data race, and the result is undefined. The Go memory model only guarantees ordering when you use channels, mutexes, atomics or other synchronisation.
The classic example is a counter incremented from many goroutines. count++ is a read, an add and a write, so concurrent updates are lost.
Running go run -race main.go reports the race with the stack traces of both goroutines. Fix it with one of the tools below.
A sync.Mutex allows one goroutine at a time into the critical section. Keep the mutex next to the data it guards and release it with defer so every return path unlocks.
A sync.RWMutex allows many readers (RLock) or one writer (Lock). It pays off only when reads clearly dominate and the critical section is not tiny, because it has more overhead than a plain mutex.
Rules that prevent common bugs:
go vet flags copies.A WaitGroup counts running goroutines. Call Add before starting the goroutine, Done when it ends, and Wait to block until the count is zero.
Since Go 1.22 each loop iteration has its own i, so the closure above is safe. Go 1.25 added wg.Go(func() { ... }), which does the Add and Done for you. For goroutines that return errors, golang.org/x/sync/errgroup is the usual choice.
sync.Once runs a function exactly once even when many goroutines call it, and all callers wait until it finishes. Since Go 1.21 there are helpers sync.OnceFunc, sync.OnceValue and sync.OnceValues.
The atomic package performs a single read, write, add or compare-and-swap that other goroutines see as indivisible. Since Go 1.19 the typed values atomic.Int32, atomic.Int64, atomic.Uint64, atomic.Bool, atomic.Pointer[T] and atomic.Value are preferred over the older function style such as atomic.AddInt64(&n, 1).
Atomics work for one variable. As soon as an update touches two fields that must stay consistent, use a mutex. Atomic compare-and-swap (CompareAndSwap) is useful for flags, state machines and publishing an immutable snapshot with atomic.Pointer.
| Need | Use |
|---|---|
| Guard a multi-field update | sync.Mutex |
| Many readers, rare writers | sync.RWMutex |
| Single counter or flag | atomic.Int64, atomic.Bool |
| Wait for goroutines | sync.WaitGroup or errgroup |
| Lazy one-time setup | sync.Once, sync.OnceValue |
| Concurrent map for disjoint keys or write-once keys | sync.Map |
| Reuse temporary objects | sync.Pool |
| Hand data between goroutines | channels |
sync.Map is not a general replacement for a map with a mutex; it is tuned for entries written once and read many times, or goroutines working on disjoint keys. In most programs a plain map plus a mutex is clearer and faster.
Use channels to pass ownership of data or to coordinate work, and mutexes to protect shared state such as a cache or counter. Neither is better in general.
For a single value, usually yes, since they avoid blocking. Measure before choosing, and use a mutex when more than one variable must change together.
Build or test with the -race flag, for example go test -race ./.... It reports races that actually happen during the run, so exercise concurrent paths in tests.
Common causes are locking a mutex twice, forgetting Unlock, calling wg.Wait without matching Done, or two goroutines taking locks in opposite order. Take locks in a consistent order.
Protect shared data with the smallest tool that fits: atomics for one value, mutexes for related fields, WaitGroup and Once for lifecycle, and channels for handing off work. Keep locks short, never copy them, and run the race detector in CI.