Golang concurrency practice problems and exercises
The part people come to Go for. Run things at the same time, safely, and stop them when you need to.
- 01GoroutinesStart ten thousand concurrent things with one keyword.Graded challenge
- 02Channels and selectPassing values between goroutines safely, and waiting on several at once.Graded challenge
- 03Mutexes and the race detectorSharing state between goroutines without corrupting it.Graded challenge
- 04ContextCancelling work that nobody is waiting for any more.Graded challenge
- 05Concurrency patternsRun once, wait for many, stop on the first error, cap the parallelism, and never leak a goroutine.
- 06The sync package in Go: every type, with examplesEvery type in `sync`, what it is for, a working example, and the mistake people make with it.
- 07Worker pools in Go: bounded concurrency, built step by stepA fixed number of goroutines pulling jobs from one channel, so 10,000 tasks never turn into 10,000 open connections.
What each lesson covers
By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.
1. Goroutines
- Write a program that starts several goroutines and waits for all of them with
sync.WaitGroup. - Predict what happens when
mainreturns before its goroutines finish. - Fix a WaitGroup bug where
Addis called inside the goroutine instead of before it.
Remember: When
mainreturns the program exits, and every goroutine dies with it.- Write a program that starts several goroutines and waits for all of them with
2. Channels and select
- Write a producer that sends on a channel and closes it, and a consumer that ranges over it.
- Explain why an unbuffered send blocks until someone receives.
- Write a
selectloop that reads from two channels until both are closed.
Remember: Know the channel behaviour table cold. Most channel deadlocks and panics are a row in it.
3. Mutexes and the race detector
- Write a struct that guards a map with a
sync.Mutex. - Explain why a data race is undefined behaviour, not just a wrong number.
- Fix a racy counter and prove it with
go test -race.
Remember: Without synchronisation, one goroutine has no guarantee of ever seeing another’s writes.
- Write a struct that guards a map with a
4. Context
- Write a function that takes
ctxfirst and returnsctx.Err()when it is cancelled. - Explain why cancelling a context does nothing unless your code checks
ctx.Done(). - Predict whether a child context made with
context.WithoutCancelstops when its parent is cancelled.
Remember: In a handler, derive from
r.Context()so a disconnecting client stops your database query too.- Write a function that takes
5. Concurrency patterns
- Write an
errgroupthat runs several tasks and returns the first error. - Write a worker pool that caps how many goroutines run at once.
- Explain how a goroutine leaks when nobody reads from its channel, and fix it.
Remember: Before you type
go, know how that goroutine will end.- Write an
6. The sync package in Go: every type, with examples
- Pick the right
synctype for a problem: lock, wait, run once, reuse, or signal. - Use
WaitGroup.Go,sync.OnceValueand the typed atomics from recent Go releases. - Name the trap in each type, such as copying a mutex or trusting
sync.Poolto keep an object.
Remember: Mutex for state, WaitGroup to wait, Once for setup, Pool for scratch memory, atomic for one value, and a channel when data moves.
- Pick the right
7. Worker pools in Go: bounded concurrency, built step by step
- Write a worker pool that returns results in input order and never leaks a goroutine.
- Stop every worker on the first error or a timeout, using a context.
- Choose between a worker pool, a semaphore and
errgroup.SetLimit, and pick a worker count with a number behind it.
Remember: Bound the work, not the goroutines: N workers, one jobs channel, and every channel closed by the side that knows it is finished.