1 min read
Reading the Go memory model so you don't have to
The five happens-before edges that cover 95% of real concurrent code.
You don't need the full memory model paper. You need these five edges:
- Goroutine start happens-before the goroutine runs. Free.
- Channel send happens-before the matching receive. Free.
- Unlock happens-before the next lock on the same mutex. Free.
sync.Once— the once-function happens-before anyDoreturn. Free.atomicoperations give sequential consistency per variable. Cheap,
but easy to misuse for anything bigger than a counter or flag.
Everything else — "it works on my machine", "it passed CI", "it's been
fine for weeks" — is a race between your data and your luck. Run the
detector: go test -race, always, in CI, from the first commit.