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.

  • Go
  • Concurrency

You don't need the full memory model paper. You need these five edges:

  1. Goroutine start happens-before the goroutine runs. Free.
  2. Channel send happens-before the matching receive. Free.
  3. Unlock happens-before the next lock on the same mutex. Free.
  4. sync.Once — the once-function happens-before any Do return. Free.
  5. atomic operations 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.