Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

Go sync vs Locks: Protecting Shared Data in Goroutines

Go
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedMar 13, 2023
UpdatedNov 17, 2026
6 min read

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.

Table of Contents

  • Locks are one part of the sync package
  • Protecting data with Mutex
  • Read-heavy data with RWMutex
  • Coordinating goroutines with the other sync primitives
  • Pitfalls that break consistency
  • Locks, atomics or channels
  • Frequently Asked Questions
    • Is a Go lock different from a sync primitive?
    • Should I use sync.Mutex or channels?
    • Why does my program deadlock with a mutex?
    • Can RWMutex cause writer starvation?
  • Summary

Locks are one part of the sync package

The sync package has two groups of types. The first group protects data. The second coordinates goroutines.

TypeGroupWhat it does
sync.MutexLockOne goroutine at a time in the critical section
sync.RWMutexLockMany readers, or one writer
sync.WaitGroupCoordinationWait until a set of goroutines finish
sync.OnceCoordinationRun a function exactly once
sync.CondCoordinationBlock until a condition is signalled
sync.PoolOptimisationReuse temporary objects
sync.MapData structureConcurrent 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.

Protecting data with Mutex

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.

go
text

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.

Read-heavy data with RWMutex

sync.RWMutex allows many goroutines to hold RLock at once, but a writer calling Lock waits for readers to leave and blocks new ones.

go

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.

Coordinating goroutines with the other sync primitives

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:

go
text

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.

Pitfalls that break consistency

  1. Copying a mutex. A sync.Mutex must not be copied after first use. Use pointer receivers and run go vet, which flags copies.
  2. Forgetting to unlock on an early return. Prefer defer mu.Unlock().
  3. Locking recursively. Go mutexes are not reentrant, so locking twice in the same goroutine deadlocks.
  4. Holding a lock during slow work such as network or disk calls. Copy the data you need, unlock, then do the slow part.
  5. Inconsistent lock order. If one goroutine locks A then B and another locks B then A, they can deadlock. Always acquire locks in the same order.

Run go test -race ./... on every concurrent package. The race detector reports unsynchronised access that tests would otherwise miss.

Locks, atomics or channels

NeedUse
Guard a struct or map used by many goroutinessync.Mutex
Mostly reads, rare writessync.RWMutex
One counter or flagsync/atomic
Pass work or results between goroutineschannels
Wait for workers to finishsync.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.

Frequently Asked Questions

Is a Go lock different from a sync primitive?

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.

Should I use sync.Mutex or channels?

Use a mutex to protect shared state accessed in short operations. Use channels to transfer data or ownership between goroutines or to signal events.

Why does my program deadlock with a mutex?

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.

Can RWMutex cause writer starvation?

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.

Summary

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.

Share