sync.RWMutex protects shared data while allowing multiple goroutines to read concurrently. RLock and RUnlock guard read-only access; Lock and Unlock exclude both readers and writers. section 7.3.4
Use RWMutex deliberately when protecting a resource such as a cache:
- Store a sync.RWMutex beside the protected data. section 7.3.4
- Use RLock and RUnlock in methods that only read. section 7.3.4
- Use Lock and Unlock in methods that modify data. section 7.3.4
- Prefer sync.Mutex by default; benchmark before choosing RWMutex for performance, because its richer interface permits locking mistakes. section 7.3.4
A cache with read/write locking
A cache can use sync.RWMutex so concurrent Read calls do not block one another, while Upsert and Delete acquire the exclusive lock. Running go test -race . verifies that the change has not introduced a data race. section 7.3.4



