Go, part 4 of 5
Go Concurrency: Goroutines, Channels and Sync
How Go runs thousands of goroutines on a few threads, how channels and select move data between them, when to reach for a mutex or an atomic instead, how context cancels work, and the patterns and leaks that come up in every senior interview. Written for a Python developer; every program was run on Go 1.27 and prints the same output on every run unless the page says otherwise.
The model and goroutines
Part 1 of 5 · what runs where, and how to start and wait for work
Go's answer to threads, the GIL and asyncio is one thing: the goroutine. This part explains what the runtime does with it and how to start, wait for and collect results from goroutines.
Concurrency, parallelism and goroutines
- Concurrency is structure: several tasks are in progress at once and their steps interleave.
- Parallelism is execution: several tasks run at the same instant on different cores.
- Go gives you concurrency with goroutines and channels; the runtime turns it into parallelism when there are free cores.
Interview line: "Concurrency is about dealing with lots of things at once, parallelism is about doing lots of things at once" (Rob Pike). asyncio is concurrent but not parallel; Go is both.
Goroutines vs OS threads
A goroutine is a function running independently, managed by the Go runtime, not by the operating system. It starts with a tiny stack that grows and shrinks as needed, so a program can hold hundreds of thousands of them.
| Goroutine | OS thread | |
|---|---|---|
| Initial stack | about 2 KB, grows by copying (up to about 1 GB on 64-bit) | fixed, often about 8 MB reserved on Linux |
| Create and switch | well under a microsecond, in user space | microseconds, needs a system call |
| Scheduled by | Go runtime | OS kernel |
| Identity | no public id, no handle, cannot be killed from outside | thread id |
| Practical count | 100,000+ in one process | a few thousand |
All sizes and timings are approximate and vary by version and platform.
The G-M-P scheduler
The runtime multiplexes many goroutines (G) onto a few OS threads (M, machine). A thread may only run Go code while it holds a P (processor), and each P has a local queue of runnable goroutines. The number of Ps is GOMAXPROCS, which defaults to the number of CPUs.
- Work stealing: a P with an empty queue takes half the goroutines of another P, then checks the global queue and the network poller.
- Blocking: a goroutine waiting on a channel, lock or network socket is parked and costs no thread. A blocking system call (file I/O, cgo) holds its M, and the runtime hands the P to another M.
- Preemption: since Go 1.14 the runtime preempts a goroutine that runs too long (about 10 ms), even inside a tight loop with no function calls.
- Containers: since Go 1.25 the default
GOMAXPROCSalso respects a Linux cgroup CPU limit.
runtime.GOMAXPROCS(0) reads the value without changing it. Set it with the GOMAXPROCS environment variable; you rarely need to.
No GIL: Go vs Python
Go has no global interpreter lock. Eight goroutines doing CPU work on an eight-core machine really use eight cores, so the Python split between threads (for I/O), processes (for CPU) and asyncio (for many sockets) disappears: goroutines cover all three. The Python side is covered on Python Concurrency.
| Goroutine | Python thread | asyncio task | Python process | |
|---|---|---|---|---|
| Start | go f() | Thread(target=f).start() | asyncio.create_task(f()) | Process(target=f).start() |
| Cost (approx.) | a few KB | an OS thread | a small object | a whole interpreter, tens of MB |
| CPU work in parallel | yes, all cores | no, the GIL (except the free-threaded build) | no, one thread | yes |
| Switch points | anywhere (runtime preempts) | anywhere (OS preempts) | only at await | anywhere |
| Blocking call inside | fine, only that goroutine waits | fine, GIL released during I/O | freezes the whole loop | fine |
| Function colouring | none: any function can be run with go | none | async spreads to callers | none, but arguments must pickle |
| Talk to each other | channels, or memory plus sync | queue.Queue, locks | asyncio.Queue | pipes, queues |
Consequence: because goroutines really run in parallel, data races are real. Code that "worked" under the GIL can corrupt data in Go. See the data race.
The go statement
go f(x) evaluates f and x now, then runs the call in a new goroutine and returns at once. There is no return value and no handle. When main returns, the program exits and every other goroutine is killed mid-step.
On almost every run the goroutine never gets to print. That is not guaranteed either way, which is the lesson: never use timing to wait. Use a WaitGroup or a channel.
sync.WaitGroup
A WaitGroup is a counter: Add(n) before starting, Done() when a goroutine ends, Wait() blocks until it is zero. Go 1.25 added wg.Go(f), which does all three for you.
- Call
Addbeforego, not inside the goroutine, orWaitmay see zero too early. - Pass a
WaitGroupby pointer; a copy has its own counter (go vetflags it). - Each goroutine writes its own slice index, so there is no race and the output is in order.
Loop variables in closures
Since Go 1.22 each loop iteration has its own variable, so a goroutine started in a loop sees the value of that iteration. The program above relies on it.
Interview history: before Go 1.22 the loop had one shared variable, so go func() { use(i) }() usually saw the last value. The fixes were i := i inside the loop or passing i as an argument. You still see both in old code; neither is needed now. Python closures still behave the old way (late binding).
Getting results back
A goroutine has no return value. Either write into a slot that belongs to it (previous chapter) or send the result on a channel. With a channel, results arrive in completion order, so sort them if order matters.
Receiving exactly len(nums) values is itself the wait; no WaitGroup is needed here.
A panic in a goroutine
An unrecovered panic in any goroutine crashes the whole program, and main cannot catch it: recover only works in the same goroutine. Unlike a Python thread, whose exception is printed and swallowed, this takes the server down.
Where a goroutine runs code you do not trust, recover inside it and turn the panic into an error:
Channels and select
Part 2 of 5 · typed pipes between goroutines
A channel is a typed, thread-safe queue that can also synchronise: a send can wait for a receiver. select waits on several channel operations at once.
Unbuffered vs buffered
make(chan T) is unbuffered: a send blocks until a receiver takes the value, so both sides meet (a rendezvous). make(chan T, n) has a queue of n: a send blocks only when the queue is full.
Unbuffered make(chan T) | Buffered make(chan T, n) | |
|---|---|---|
| Send blocks until | a receiver takes it | there is free space |
| Receive blocks until | a sender offers a value | there is a value in the queue |
| Guarantee | after the send, the receiver has the value | only that the value is queued |
| Use for | hand-off and signalling | smoothing bursts, known number of results, semaphores |
| Python analogy | no direct one | queue.Queue(maxsize=n) |
Gotcha: a buffer does not fix a design that deadlocks; it only delays the deadlock until the buffer fills. Pick a size for a reason (known count, bounded burst), not to "make it work".
Send, receive, close and range
close(ch) says "no more values". Receivers drain what is left, then get the zero value at once. The comma-ok form tells a real zero from "closed", and for v := range ch stops by itself after close.
- Closing is a broadcast: every receiver waiting on the channel wakes up. That is why it is used for "stop" signals.
- You do not have to close a channel to free it; the garbage collector does that. Close only to tell receivers you are finished.
Directional channel types
In a signature, chan<- T is send-only and <-chan T is receive-only. A plain chan T converts to either automatically. It documents who does what, and the compiler enforces it.
Sending inside sum does not compile:
Only a sender can close: closing a receive-only channel is also a compile error.
The channel axioms
The most asked channel question. Every cell below was checked by running it on Go 1.27.
| Operation | nil channel | open channel | closed channel |
|---|---|---|---|
Send ch <- v | blocks forever | sends, or blocks until room / receiver | panic: send on closed channel |
Receive <-ch | blocks forever | receives, or blocks until a value | remaining values, then zero value at once, ok == false |
Close close(ch) | panic: close of nil channel | closes; waiting receivers wake | panic: close of closed channel |
A nil channel looks useless, but "blocks forever" is handy in select: setting a channel variable to nil switches that case off. See select.
Who closes
- The sender closes, never the receiver. Only the sender knows there is nothing more, and a receiver that closes makes the sender panic.
- Several senders: none of them may close. Wait for all of them with a
WaitGroup, then close from one extra goroutine (see fan-in). - Receiver wants to stop early: it does not close the data channel; it cancels a
contextor closes a separate done channel that the senders watch.
"All goroutines are asleep"
If every goroutine is blocked, the runtime knows nothing can ever wake them and stops the program. The classic cause is a send on an unbuffered channel with nobody receiving.
Gotcha: the detector only fires when all goroutines are stuck. In a server, other goroutines (the HTTP listener, timers) are alive, so a stuck goroutine just hangs silently. That is a goroutine leak, and nothing reports it.
select
select waits until one of its cases can proceed and runs it. If several are ready, it picks one at random, so no case starves. This merge loop also uses the nil-channel trick: a closed input is set to nil so its case never fires again.
The order in which values arrive differs between runs, so the program prints only the count and the sum. An empty select {} blocks forever.
Non-blocking with default
A default case runs when no other case is ready, so the select never blocks. Typical use: drop or count work when a queue is full instead of stalling the caller.
Gotcha: a for { select { ... default: } } loop with nothing else to do spins one core at 100%. Use default only for one-shot "try" operations.
Timeouts with time.After
time.After(d) returns a channel that receives once after d. Racing it against the real work in a select gives a timeout.
The buffer of 1 matters: with an unbuffered channel the abandoned goroutine would block on its send forever. In real code prefer context.WithTimeout, which also tells the slow work to stop.
Done channels and tickers
A chan struct{} carries no data; closing it tells every listener to stop. time.NewTicker delivers a value every period; always Stop it when finished.
context.Context is the standard, richer version of a done channel: ctx.Done() is exactly such a channel.
Shared memory and sync
Part 3 of 5 · races, locks and atomics
Channels are not the only tool. When goroutines share a value, protect it with the sync and sync/atomic packages, and prove it with the race detector.
The data race
count++ is three steps: read, add, write. Two goroutines can read the same old value and both write old+1, so increments are lost. This output is different on every run; that is the point.
In Go a data race is not just a wrong number: the memory model gives no guarantees for racy programs, and a race on a map, slice or interface can crash. Python's GIL made count += 1 in threads usually look fine; in Go it never is.
The race detector
Add -race to go run, go test or go build. It instruments every memory access and reports two goroutines touching the same address without synchronisation, with both stacks. Here is the program above (trimmed):
- It finds races that happen during the run, so run your tests with
-racein CI. - It costs roughly 2 to 20 times the CPU and 5 to 10 times the memory, so it is for tests, not production.
- A program with a detected race exits with status 66.
sync.Mutex: a safe account
A Mutex lets one goroutine at a time into a critical section. Keep the mutex next to the data it guards, lock in the methods, and unlock with defer so an early return or panic cannot leave it locked.
- The zero value of
sync.Mutexis an unlocked mutex; no constructor needed. - Go mutexes are not reentrant: locking twice in the same goroutine deadlocks (Python's
RLockhas no Go equivalent, on purpose). - Use pointer receivers. A value receiver copies the mutex, see copying a mutex.
sync.RWMutex
An RWMutex allows many readers at once or one writer. It pays off only when reads are far more common than writes and the critical section is not tiny; otherwise a plain Mutex is as fast and simpler.
sync/atomic
For a single counter or flag, the typed atomics (atomic.Int64, atomic.Bool, atomic.Pointer[T], Go 1.19+) are faster than a mutex and cannot be misused by forgetting to lock. This fixes the racy counter.
Limit: atomics protect one value. "Check balance, then subtract" is two steps over related data and needs a mutex.
Once, Pool and Map
sync.Once runs a function exactly once, however many goroutines call it at the same time: lazy initialisation without races. sync.OnceValue (Go 1.21+) wraps a function that returns a value.
sync.Pool: a cache of reusable temporary objects (buffers) to cut allocations on hot paths. The GC may empty it at any time, so never store state in it.sync.Map: a concurrent map tuned for keys written once and read many times, or goroutines touching disjoint keys. For anything else, a plainmapplus aMutexis clearer, typed and usually as fast.
Deadlock from lock ordering
A transfer must lock both accounts. If one goroutine moves money from A to B while another moves from B to A, each can hold its first lock and wait forever for the second. The sleep only widens the window so the demo deadlocks every time.
The fix: every goroutine takes the locks in the same global order, here by account id. Then no cycle of waiting is possible.
Interview line: the four deadlock conditions are mutual exclusion, hold and wait, no preemption and circular wait. Consistent lock ordering removes circular wait. A same-account transfer (from == to) needs a separate check, or it locks one mutex twice.
Channels or a mutex?
The Go proverb is "do not communicate by sharing memory; share memory by communicating". It is advice, not a rule: the standard library uses mutexes everywhere.
| Situation | Pick |
|---|---|
| Handing data or ownership from one goroutine to another | channel |
| Pipelines, worker pools, fan-out / fan-in | channels |
| Signalling "done" or "stop" to many goroutines | close a channel, or context |
| Waiting for N goroutines to finish | WaitGroup |
| Shared state: a cache, a balance, a map of sessions | Mutex / RWMutex |
| A single counter or flag | atomic |
| One-time initialisation | sync.Once / OnceValue |
Rule of thumb: if you are building a struct with a channel only to guard a field, use a mutex.
Context and patterns
Part 4 of 5 · cancellation and the shapes real code uses
context carries a cancel signal and a deadline down a call tree. The patterns after it are the ones interviewers ask you to write on a whiteboard.
context and WithCancel
A goroutine cannot be killed from outside; it must be asked to stop and must check. context.WithCancel(parent) returns a child context and a cancel function. Calling cancel closes ctx.Done() for that context and every context derived from it, and ctx.Err() then says why.
- Pass
ctx context.Contextas the first parameter, namedctx. Do not store it in a struct. - Always call
cancel, usually withdefer, even if the work finished; otherwise the context's resources live until the parent ends (go vetwarns about a lost cancel). context.Background()is the root inmainand tests;context.TODO()marks "I have not plumbed a context here yet".WithCancelCauseandcontext.Cause(Go 1.20+) attach your own error as the reason.
WithTimeout and WithDeadline
WithTimeout(ctx, d) cancels itself after d; WithDeadline(ctx, t) at a wall-clock time. A child can only shorten its parent's deadline, never extend it. Slow work must select on ctx.Done() to notice.
Why it matters: in an HTTP server, r.Context() is cancelled when the client disconnects. Pass it to db.QueryContext(ctx, ...) and http.NewRequestWithContext(ctx, ...) and an abandoned request stops its database query and outbound calls instead of finishing work nobody will read. It is the Go version of cancelling an asyncio task, but explicit and cross-library.
Context values
context.WithValue attaches request-scoped data such as a request id or trace id that crosses API boundaries. Use an unexported key type so packages cannot collide. Do not use it for optional function parameters, database handles or config: values are untyped, invisible in signatures and slow to look up through a long chain.
A worker that stops on cancel
The standard loop: select on the work channel and on ctx.Done(). The worker exits on whichever comes first.
Worker pool
A fixed number of workers read from one jobs channel and write to one results channel. The pool size bounds concurrency (database connections, API quota). Closing jobs ends the workers; closing results after they all finish ends the collector.
Python equivalent: ThreadPoolExecutor(max_workers=3).map(...). Go has no built-in pool because this is ten lines.
Pipeline
Each stage is a goroutine that reads from an input channel, does one thing, writes to an output channel and closes it when its input ends. Stages run at the same time, like a Unix pipe or chained Python generators, but in parallel.
One goroutine per stage keeps order. If the sink stops early, upstream stages block on send and leak; real pipelines also pass a ctx and select on ctx.Done() in every send.
Fan-out / fan-in
Fan-out: several goroutines read from the same channel to share slow work. Fan-in: merge several channels into one. The merge closes its output only after all inputs are drained, using a WaitGroup.
Bounding concurrency with a semaphore
When you do want one goroutine per item but at most N running at once, a buffered channel of capacity N is a counting semaphore: send to acquire a slot, receive to release it. This is the fix for unbounded goroutine creation.
Python equivalent: asyncio.Semaphore(3) or threading.BoundedSemaphore(3).
First error cancels the rest
Run tasks in parallel; if any fails, cancel the others and return that first error. The golang.org/x/sync/errgroup package does exactly this (g.Go, g.Wait, SetLimit), and most projects use it. Here is the idea with the standard library only, which is what interviewers want to see.
The program ends in milliseconds, not one second: the four slow tasks see ctx.Done() as soon as task 3 fails. sync.Once makes sure only the first error is kept.
Rate limiting with a ticker
Waiting on a ticker before each call spaces calls evenly, for example to respect a payment gateway's limit. For bursts and token buckets use golang.org/x/time/rate.
Graceful shutdown
signal.NotifyContext returns a context that is cancelled on Ctrl-C or SIGTERM (what Kubernetes sends). Workers watch it, finish their current item and return; main waits for them before exiting. This program sends itself an interrupt so it ends on its own.
With an HTTP server, call srv.Shutdown(shutdownCtx) after ctx.Done(): it stops accepting connections and waits for in-flight requests, up to the timeout in shutdownCtx.
Pitfalls and interview
Part 5 of 5 · leaks, misuse, and the questions you will be asked
The bugs below compile, usually pass tests, and show up in production as memory growth or a hang.
Goroutine leaks
A goroutine blocked forever is never collected, nor is anything it references. The usual cause is a sender whose receiver gave up (a timeout, an early return). runtime.NumGoroutine() makes the leak visible.
- Main plus 3 stuck goroutines is 4, and the fixed version adds none.
- In a service, watch the goroutine count metric or the
/debug/pprof/goroutineprofile; a count that only grows is a leak. - Every goroutine you start needs an answer to "how does it end?". Usually: its input closes or its
ctxis cancelled. - In tests,
testing/synctest(Go 1.25+) can run code in a bubble and report goroutines that stay blocked.
Close mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Forgetting to close a channel that is ranged over | the range never ends: deadlock in a small program, a leaked goroutine in a server | defer close(out) as the first line of the producing goroutine |
| Closing twice | panic: close of closed channel | exactly one owner closes; if several paths can, wrap it in sync.Once |
| Closing from the receiver | the sender panics with send on closed channel | signal the sender through a context or done channel instead |
| Several senders, one closes | the others panic | WaitGroup over the senders, close after Wait |
Copying a mutex
A struct that contains a sync.Mutex (or WaitGroup, Once, atomics) must not be copied after first use: the copy gets its own lock, so the original is no longer protected. Passing it by value or using a value receiver copies it. go vet catches it.
Fix: func show(a *Account), and pointer receivers on every method. go test runs this vet check automatically.
Unbounded goroutines and time.After in loops
- One goroutine per incoming item, with no limit. Goroutines are cheap, but what they do is not: a million at once each opening a database connection or holding a buffer will exhaust connections, memory or the remote API. Bound it with a worker pool or a semaphore.
time.Afterinside a hotfor/selectloop makes a new timer each iteration. Since Go 1.23 unreferenced timers are collected promptly, so this is no longer a leak, but a singletime.NewTimerwithReset, or a context deadline, is still cleaner.- Sleeping to wait for goroutines (as in the go statement) passes on your laptop and fails under load. Synchronise, never sleep.
Interview questions
| Question | Short answer |
|---|---|
| Goroutine vs thread? | A goroutine is scheduled by the Go runtime onto a few OS threads, starts with a ~2 KB growable stack and costs far less to create and switch. |
| What is G-M-P? | Goroutines, OS threads (machines) and processors. GOMAXPROCS Ps each hold a run queue; an M needs a P to run Go code; idle Ps steal work. |
| Does Go have a GIL? | No. Goroutines run in parallel on all cores, which is why data races are a real risk. |
| Unbuffered vs buffered channel? | Unbuffered: send waits for a receiver (synchronises). Buffered: send waits only when full. |
| Send on a closed channel? Receive? | Send panics. Receive returns remaining values, then the zero value with ok == false. |
| What does a nil channel do? | Send and receive block forever; close panics. In select it disables that case. |
| Who should close a channel? | The single sender, when it is done. With many senders, close after a WaitGroup wait. |
| How do you stop a goroutine? | You cannot kill it. Cancel a context (or close a done channel) and have it check ctx.Done(). |
| How do you find a data race? | go test -race. Fix with a mutex, an atomic, or by giving the data a single owner goroutine. |
| Mutex or channel? | Channel to pass ownership or coordinate stages; mutex to guard shared state. |
| What is a goroutine leak and how do you spot it? | A goroutine blocked forever, usually on a send nobody receives. Spot it with runtime.NumGoroutine or the pprof goroutine profile. |
What does select do when several cases are ready? | Picks one at random; with default it never blocks. |
Cheat sheet
| Need | Go |
|---|---|
| Start work | go f(x) |
| Wait for N goroutines | var wg sync.WaitGroup; wg.Go(f); wg.Wait() |
| Make a channel | make(chan T), make(chan T, n) |
| Send, receive, check closed | ch <- v, v := <-ch, v, ok := <-ch |
| Read until closed | for v := range ch |
| Signature direction | out chan<- T, in <-chan T |
| Wait on several channels | select { case ...: default: } |
| Timeout | context.WithTimeout(ctx, d), or case <-time.After(d): |
| Cancel | ctx, cancel := context.WithCancel(ctx); defer cancel() |
| Stop on Ctrl-C / SIGTERM | signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) |
| Guard shared state | mu.Lock(); defer mu.Unlock(), RLock for readers |
| Counter or flag | atomic.Int64, atomic.Bool |
| Run once | sync.Once, sync.OnceValue(f) |
| Limit concurrency | sem := make(chan struct{}, n) |
| Find races | go test -race ./... |
| Count goroutines | runtime.NumGoroutine() |
Next: Go Practice: Exercises with Solutions, small problems that use everything from the first four pages.