Lesson 14 · Concurrency & context
Goroutines
The cheap concurrent function — and the interviewer's favourite distinction.
Your win: launch a goroutine, crisply distinguish concurrency from parallelism, and explain why every goroutine you start needs a way to stop.
A goroutine is a function running concurrently
Prefix a call with go and it runs in a new goroutine,
scheduled by the Go runtime — not the OS — onto a small pool of OS threads. Goroutines
start with a tiny stack (a few KB) that grows on demand, so you can have hundreds of
thousands.1
go handleMessage(msg) // returns immediately; the work runs concurrently
The launching code does not wait — you need a channel or a
WaitGroup (Lessons 15–16) to know when the goroutine is done. Our Kafka
engine launches one goroutine per consumer instance and tracks them with a
WaitGroup:
k.consumerWaitGroup.Add(1)
go func(member consumer, h MsgHandler) {
defer func() { k.consumerWaitGroup.Done(); ... }() // signal completion
k.runConsumer(spanName, member, h)
}(member, handleMsg) // args passed in, not captured — see below
Concurrency is not parallelism
GOMAXPROCS cores, but that's a separate concern.2
Why it matters: you design for concurrency (structure your program as independent pieces that communicate); parallelism is a runtime bonus you mostly don't manage by hand. Our Kafka consumers are structured concurrently — whether two run on two cores in parallel is the scheduler's call.
Every goroutine needs an exit
ctx.Done() or a close
channel (Lessons 15, 17).
Our consumers embody this: each runs a loop that selects on its
context's Done(), and Close() cancels every consumer context
then Wait()s — a clean shutdown, no leaks (the full pattern is Lesson 18).
member/h as parameters
rather than capturing loop variables. Before Go 1.22 that was mandatory — a
captured loop variable was shared across iterations, a classic bug. Go 1.22 (and our
1.25) made each iteration's variable fresh, so it's fixed — but the repo still passes
args explicitly, and interviewers still ask about the old trap.
Rob Pike — "Concurrency is not Parallelism"
The definitive framing, from a Go creator (~30 min). Then do the Tour's concurrency section to launch your first goroutines.
Check yourself (from memory)
Q1. "Concurrency is not parallelism" means…
Q2. A goroutine that can never exit causes…
ctx.Done()/close-channel exit.
Q3. Compared to an OS thread, a goroutine is…
GOMAXPROCS cores, but that's separate — you design for concurrency.1. Effective Go — goroutines; A Tour of Go.
2. Rob Pike, Concurrency is not Parallelism.