Concurrency From the Ground Up
Concurrency From the Ground Up
This part is a diagram-first curriculum for Go concurrency: goroutines, channels, pipelines, time, context, wait groups, races, semaphores, atomics, testing, a simplified scheduler model, race-condition reasoning, and condition-variable signaling.
It is designed for programmers who already know Go basics (types, interfaces, errors) and want the application of concurrency primitives—not a feature laundry list.
Pedagogy note. The chapter map and teaching emphasis follow the structure popularized by interactive concurrency guides such as Gist of Go: Concurrency (Anton Zhiyanov). Explanations, diagrams, and code here are original to this book; they are not a copy of that site. Use both: that site for interactive exercises, this part for a library-style reference with ASCII diagrams integrated into the monorepo.
Tooling baseline (late 2026): examples assume a recent Go toolchain (Go 1.27-class: WaitGroup.Go, generic methods, go test -race). Use go mod init with go 1.27 when creating lab modules.
Learning Outcomes
After this part you can:
- Separate business logic from concurrency mechanics
- Choose among wait groups, done/cancel channels, and context
- Build cancel-safe pipelines without leaked goroutines
- Apply throttling, backpressure, and timeouts correctly
- Detect data races and fix them with ownership, mutexes, or atomics
- Distinguish race conditions from data races (including
-raceclean logic bugs) - Use channels first; reach for
sync.Condonly when predicates need broadcast - Explain (at a teaching level) how Gs map onto OS threads
Contents Map
Basics: goroutines → channels → pipelines → time → context
Sync: wait groups → races/mutex → semaphores → atomics
Practice: testing → scheduler model → race conditions → Cond
| Chapter | File | Focus |
|---|---|---|
| 251 | 251-goroutines-fundamentals | go, main lifetime, WaitGroup, first channels |
| 252 | 252-channels-complete | close, range, directions, done, deadlocks, nil channels |
| 253 | 253-pipelines-select-cancel | leaks, cancel+select, merge, fan-out/fan-in |
| 254 | 254-time-throttle-backpressure | throttle, backpressure, timeouts, timers |
| 255 | 255-context-complete | context tree, deadline, values, vs cancel channel |
| 256 | 256-waitgroups-encapsulation | WaitGroup mental model, pointers, encapsulation |
| 257 | 257-data-races-mutex | data races, race detector, mutex, RWMutex |
| 258 | 258-semaphores-rendezvous | semaphore ≤N, rendezvous, barriers |
| 259 | 259-atomics-complete | atomic RMW, composition traps, CAS |
| 260 | 260-testing-concurrent-code | testing strategies, determinism |
| 261 | 261-scheduler-model-diagrams | cores → threads → Gs, GOMAXPROCS |
| 262 | 262-race-conditions-vs-data-races | race conditions vs data races |
| 263 | 263-signaling-cond-broadcast | sync.Cond, Signal vs Broadcast |
Suggested path
Week-shaped path (adjust pace):
Day 1–2 251 → 252 start + talk
Day 3–4 253 → 254 → 255 pipelines + time + context
Day 5 256 → 257 WaitGroup + races
Day 6 258 → 259 limits + atomics
Day 7 260 → 261 test + scheduler model
Day 8 262 → 263 logic races + Cond
Always keep go test -race (or go run -race) in the loop for shared-memory chapters (257+).
Core Mental Models (Preview)
Three layers of concurrency
instr instr instr instr
┌──────┐┌──────┐┌──────┐┌──────┐
│Core 1││Core 2││Core 3││Core 4│ CPU (true parallel ops)
└──────┘└──────┘└──────┘└──────┘
▲ ▲ ▲ ▲
┌──────┐┌──────┐┌──────┐┌──────┐
│ Thr A││ Thr B││ Thr C││ Thr D│ OS threads (OS schedules)
└──────┘└──────┘└──────┘└──────┘
▲ ▲ ▲ ▲
G11 G15 G12 G16 G13 G14 G17… Goroutines (Go schedules)
Channel as data + synchronization
┌─────────────┐ ┌─────────────┐
│ goroutine A │ ch │ goroutine B │
│ recv │◄────────│ send │
└─────────────┘ └─────────────┘
unbuffered send blocks until recv (rendezvous)
Cancel vs done
done: worker ──signals──► waiter "I finished"
cancel: waiter ──signals──► worker "stop now"
Data race vs race condition
data race: concurrent mem access, ≥1 write, no happens-before
race condition: wrong result due to interleaving (may be -race clean)
See 262.
How to Study
- Read a chapter, redraw one diagram from memory.
- Run every runnable example; then break it (remove close, drop cancel, share a map).
- Prefer one solid pipeline lab over ten passive reads.
- Always keep
go test -racein the loop for Part B (sync and later). - When stuck, write the sequential version first, then add concurrency at the edges.
Lab module template
mkdir conc-lab && cd conc-lab
go mod init example.com/conc-lab
# go.mod: set `go 1.27` (or your installed version)// go.mod
module example.com/conc-lab
go 1.27go run -race .
go test -race ./...Relationship to Other Parts
| Need | Also see |
|---|---|
| Runtime-deep GMP / netpoll / timers | 20 Go Deep Dives |
| Worker pools in production services | 07 Concurrency |
Stdlib sync / context recipes |
98 Stdlib |
| Capstone-style builds | 99 Projects |
Attribution
Teaching structure inspired by public concurrency curricula (notably antonz.org’s Gist of Go: Concurrency). All prose, diagrams, and examples in this monorepo chapter set are written for this book under the same author voice as the rest of the Go library.