In Go, locks are not separate from sync: sync.Mutex and sync.RWMutex are the locks, and they live inside the sync package next to other primitives such as WaitGroup, Once, Cond and Pool. The real difference is scope. A lock only guards shared data from simultaneous access, while the wider set of sync primitives also coordinates when goroutines start, wait and finish.
To keep shared data consistent, you pick a lock (or an atomic, or a channel) for protection and add the other sync types when goroutines need to cooperate.
The sync package has two groups of types. The first group protects data. The second coordinates goroutines.
| Type | Group | What it does |
|---|---|---|
sync.Mutex | Lock | One goroutine at a time in the critical section |
sync.RWMutex | Lock | Many readers, or one writer |
sync.WaitGroup | Coordination | Wait until a set of goroutines finish |
sync.Once | Coordination | Run a function exactly once |
sync.Cond | Coordination | Block until a condition is signalled |
sync.Pool | Optimisation | Reuse temporary objects |
sync.Map | Data structure | Concurrent map for narrow access patterns |
A lock answers the question "who may touch this data right now?". A WaitGroup answers "have all my workers finished?". Mixing the two up leads to code that is either racy or deadlocked.
A Mutex makes a block of code exclusive. Keep the mutex next to the data it guards, and lock with defer so every return path unlocks.
Without the mutex, concurrent writes to a map would be a data race and the runtime can terminate the program with a fatal error. Here the lock protects the map and the WaitGroup makes main wait.
sync.RWMutex allows many goroutines to hold RLock at once, but a writer calling Lock waits for readers to leave and blocks new ones.
RWMutex has more bookkeeping than Mutex. Use it only when reads clearly dominate and the critical section is not trivial; otherwise a plain Mutex is often faster. You cannot upgrade a read lock to a write lock, so release RLock before calling Lock.
When the problem is timing rather than exclusion, a lock is the wrong tool. Use WaitGroup to wait for workers and Once for lazy setup:
sync.OnceValue (Go 1.21) runs the function once, caches the result and is safe for any number of goroutines. In Go 1.25 and later, wg.Go(func() { ... }) also replaces the manual Add and Done pair.
sync.Mutex must not be copied after first use. Use pointer receivers and run go vet, which flags copies.defer mu.Unlock().Run go test -race ./... on every concurrent package. The race detector reports unsynchronised access that tests would otherwise miss.
| Need | Use |
|---|---|
| Guard a struct or map used by many goroutines | sync.Mutex |
| Mostly reads, rare writes | sync.RWMutex |
| One counter or flag | sync/atomic |
| Pass work or results between goroutines | channels |
| Wait for workers to finish | sync.WaitGroup |
The Go proverb "share memory by communicating" favours channels when ownership moves, but a mutex around shared state is idiomatic and often clearer when many goroutines read and update the same structure.
No. Mutex and RWMutex are sync primitives. People usually say "sync" to mean the broader coordination types and "locks" to mean the two mutex types.
Use a mutex to protect shared state accessed in short operations. Use channels to transfer data or ownership between goroutines or to signal events.
The usual causes are locking twice in the same goroutine, inconsistent lock ordering, or a missing Unlock on one code path. The runtime reports "all goroutines are asleep" only when every goroutine is blocked.
Go's RWMutex blocks new readers once a writer is waiting, so writers are not starved. Heavy reader traffic can still delay a writer until the current readers finish.
Locks (sync.Mutex, sync.RWMutex) are the data-protection part of the sync package. The rest of sync handles coordination: waiting, one-time setup, conditions and object reuse. Pick the narrowest tool for the job, keep critical sections short, never copy a mutex, and verify the result with go test -race.