We break down what kinds of mutable state exist in a Go application at different layers (
app,
storage,
cache), who owns the data, and which synchronization primitives to choose:
Mutex for invariants,
RWMutex for frequent reads,
atomic for simple counters. With examples, we cover operation deduplication, in-memory storage, file writes, a cache wrapper with invalidation, and common mistakes (a global lock, long critical sections, and leaking internal structures to the outside).