Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Concepts

The Go memory model

Understand the Go memory model: what happens-before means, which operations synchronize goroutines (channels, mutexes, Once, atomics) and why data races are bugs.

The Go memory model answers one question: when a goroutine reads a variable that another goroutine writes, which value is it guaranteed to see? The answer is built on the relation happens-before. If a write happens before a read, and no other write intervenes, the read sees that write. If neither happens before the other, they are concurrent, and if at least one of them is a write, you have a data race.

Modern compilers and CPUs reorder instructions and cache values per core. Without synchronization, goroutines may observe stale or even partially written values. Synchronization operations create the happens-before edges that make shared data safe.

Go
var data int
var ready bool
// goroutine A: data = 1; ready = true
// goroutine B: for !ready {}; print(data) // NOT guaranteed to print 1

Try it

Go Channel-ordered publication

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Mutex, Once and atomic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The synchronization edges

Operation What happens before what
go f() the go statement happens before the start of f
goroutine exit no guarantee: exit is not synchronized with anything on its own
channel send / receive a send happens before the matching receive completes
channel close the close happens before a receive that returns because of it
unbuffered channel a receive happens before the matching send completes
buffered channel, capacity C the k-th receive happens before the (k+C)-th send completes
sync.Mutex / RWMutex the n-th Unlock happens before the (n+1)-th Lock returns
sync.Once the f call in Do(f) completes before any Do returns
sync.WaitGroup each Done happens before the Wait it releases returns
sync/atomic an atomic write observed by an atomic read happens before that read (sequentially consistent)

Usage notes

  • A data race is undefined in the sense that matters: a racy program has no guaranteed meaning. Go limits the damage (word-sized races yield some previously written value) but multiword values like slices, strings and interfaces can tear and crash the program.
  • Detect races with go test -race or go run -race. The detector only finds races that happen in the run, so test concurrent code under load.
  • The compiler may assume no races. Busy-waiting on a plain variable (for !ready {}) can loop forever or print stale data.
  • Prefer sharing memory by communicating: pass ownership through a channel instead of locking shared state when it fits the design.
  • Reads of map while another goroutine writes cause a fatal error ("concurrent map read and map write"), not just stale data.
  • Atomics give synchronization for the values they guard and, in Go's model, for the data written before an atomic store that another goroutine sees by an atomic load. Still, use a mutex when several variables must change together.
  • Initialization: package init functions happen before main.main; an imported package's init completes before the importer starts.