ZZomato·BehavioralL3System Design

Mutex vs Channels in Golang

viaGlassdoor

Q: Mutex vs Channels in Go - when do you use each? A: A Mutex (sync.Mutex/RWMutex) directly protects shared memory by allowing only one goroutine to hold the lock at a time around a critical section - best for simple shared-state protection (e.g., protecting a counter or map). Channels implement Go's 'share memory by communicating' philosophy: goroutines pass data through a channel rather than directly sharing state, which is often clearer for coordinating pipelines, worker pools, or signaling completion (e.g., using a channel as a semaphore or for fan-in/fan-out patterns). Rule of thumb: use mutexes for simple protection of shared state accessed by many goroutines; use channels when you need to coordinate control flow or pass ownership of data between goroutines.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account