`sync.Mutex` is a mutual exclusion lock used to protect shared data from concurrent access.
package main import "sync" type Counter struct { mu sync.Mutex n int } func (c *Counter) Inc() { c.mu.Lock() c.n++ c.mu.Unlock() } func (c *Counter) Value() int { c.mu.Lock() defer c.mu.Unlock() return c.n }