0Pricing
Go Academy · Lesson

The Go Memory Model and Happens-Before

Synchronization guarantees and memory ordering

The Go Memory Model and Happens-Before is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is the memory model?

Go's memory model defines when writes to a variable by one goroutine are guaranteed to be visible to reads by another. Without these guarantees, concurrent programs can observe stale or inconsistent values.

Happens-before

A happens-before relationship guarantees that all writes before event A are visible at event B. If no happens-before exists, reads may observe any write — including the zero value.

Guaranteed happens-before: goroutine start

Everything that happens before a go statement happens before the goroutine function begins. The spawned goroutine sees all writes made before its creation.

x := 10
go func() {
    fmt.Println(x) // guaranteed to see 10
}()

Guaranteed: channel send/receive

A send on a channel happens before the corresponding receive. A close happens before a receive of the zero value. These are the primary synchronisation primitives.

ch := make(chan int)
go func() { ch <- 42 }()   // send hb receive
v := <-ch
fmt.Println(v) // guaranteed to see 42

Guaranteed: sync.Mutex

The n-th Unlock of a mutex happens before the (n+1)-th Lock. This ensures that a goroutine locking after an unlock sees all writes made before the unlock.

Guaranteed: sync.Once

The completion of the once.Do function happens before any call to once.Do returns. All goroutines that call once.Do see the initialisation.

Guaranteed: channel close

Closing a channel happens before a receive that returns the zero value from the closed channel. This is how a done channel broadcasts to many goroutines.

No happens-before: unprotected shared variables

If two goroutines access a variable without synchronisation and at least one writes, the behaviour is undefined. The race detector catches this.

Atomic operations

The atomic package provides sequentially consistent operations. An atomic store happens before an atomic load of the same variable in any goroutine that observes the stored value.

Memory model for channels

For buffered channels of capacity C, the k-th receive happens before the (k+C)-th send completes. This models the limited buffer capacity guaranteeing backpressure.

Practical rule

If you share data between goroutines: use channels to transfer ownership, or use mutexes/atomics to protect reads and writes. Do not rely on intuition about ordering without synchronisation.

Quick Check

Which of the following creates a happens-before relationship between goroutines?

Recap: Go Memory Model

Key points:

  • Happens-before defines when writes are visible across goroutines
  • Channels, Mutex, Once, and goroutine start create HB edges
  • Unsynchronised access to shared mutable state is undefined
  • Use the race detector to catch violations

Frequently asked questions

Is the “The Go Memory Model and Happens-Before” lesson free?

Yes — the full text of “The Go Memory Model and Happens-Before” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Go Memory Model and Happens-Before”?

Synchronization guarantees and memory ordering You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Go Memory Model and Happens-Before” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Stack vs Heap and Escape Analysis
  2. The Go Memory Model and Happens-Before
  3. Garbage Collector Internals
  4. Reducing Allocations: sync.Pool and Arenas
← Back to Go Academy