Go offers two approaches to concurrency safety: channels ("share memory by communicating") and the sync package (mutexes/locks for protecting shared memory directly). Despite Go's channel-first philosophy, sync primitives are often the simpler, more efficient choice for protecting shared state.
sync.Mutex — protect shared state with a lock
Counter {
mu sync.Mutex
count
}
Increment() {
c.mu.Lock()
c.mu.Unlock()
c.count++
}
