Go Academy · Lesson

The Go Memory Model and Happens-Before

Synchronization guarantees and memory ordering

Lesson 2 of 413 steps

The Go Memory Model and Happens-Before is a free Go Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

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
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the Go Academy learning path, including “The Go Memory Model and Happens-Before”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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