Go Concurrency
What happens when there are two
A Go program with one goroutine is a program. A Go program with two has an ordering you did not write, a value two things can reach, and a whole class of failure that shows up as a clean exit. This is how to write the second kind on purpose — and how to prove it works rather than hoping.
- Modules
- 6
- Drills
- 9
- Build steps
- 7
- Time
- ~5h
Walk out able to
Build a worker pool that is bounded, cancellable on a deadline, accounts for every job, and is silent under the race detector.
What you build and drill
Goroutines and channels
Launch, join, hand values over, and close without panicking.
select and context
Wait on several at once; cancel a whole tree on a deadline.
The sync package
Guard what stays put, and know when a channel was wrong.
Races, proved gone
Read a -race report, and tell a deadlock from a leak.
The pitch
What you practise, and what you leave with
fanout — four workers over forty jobs, stopped by a deadline, every job either collected or counted, and nothing left blocked when it exits.
You will practise
- Goroutines, joining, and what a closure saw
- Channels: blocking, buffering, closing
- select, timeouts, and context cancellation
- Mutexes, data races, and the notation itself
Afterwards you can
- Say why a program printed nothing
- Cancel a goroutine tree on a deadline
- Choose a channel or a mutex on purpose
- Read a -race report and fix what it names
Modules
6 modules, 38 items
Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.
- 1
Start it, then wait for it
~59 minLaunch goroutines, join them, and say what each one could see when it ran.
- LessonWhy a second goroutine changes things5 min
- LessonThe program that prints nothing6 min
- LessonCheap, and not the same as parallel8 min
- Build stepLaunch four, and wait for none7 min
- Build stepNow wait for all four8 min
- DrillHow many lines does it print11 min
- LessonWhat each goroutine can see7 min
- LessonJoin without losing any7 min
- 2
Hand a value over
~51 minPredict whether any send or receive blocks, and close a channel without panicking.
- LessonA send is a meeting8 min
- LessonWhat a buffer changes7 min
- DrillHow much got through11 min
- LessonClose means "no more values"6 min
- LessonWho is allowed to close7 min
- DrillHow does this program end12 min
- 3
Wait on several at once
~41 minRead a select, put a timeout on anything, and stop a loop spinning.
- LessonFour rules and a coin toss7 min
- LessonTimeouts and the case you switch off8 min
- DrillWhich case runs11 min
- Build stepFive workers, and wait for every one7 min
- Build stepPut a deadline on it8 min
- 4
Stop work nobody wants
~43 minCancel a goroutine tree on a deadline, and say what its error will be.
- LessonCancellation is a closed channel6 min
- LessonWhat a context adds to that7 min
- LessonThe three conventions6 min
- DrillWhat will ctx.Err() say11 min
- Build stepGive up, and say why in one word13 min
- 5
When sharing is simpler
~51 minGuard state that stays put, and say when a channel was the wrong tool.
- LessonPut the lock beside what it guards6 min
- LessonA lock does more than exclude6 min
- LessonThree more, and the bug none of them fix7 min
- DrillChannel, mutex, or neither12 min
- DrillWhat the counter ends on10 min
- DrillType the concurrency you mean10 min
- 6
Prove it
~1hFind a race with the detector, and build a pool that has none.
- LessonWhat counts as a data race6 min
- LessonRead a report the detector wrote7 min
- LessonStopped, or merely stuck6 min
- DrillWhere the race is, if there is one12 min
- Build stepWatch the detector find one13 min
- Build stepBuild the pool, and prove it20 min
- LessonWhat the pool was made of6 min
- LessonWhere the next layer lives4 min
More in Languages
Keep going
Python Foundations
Read a program before you run it. Then write one worth showing.
Foundational~6h8 modules
Python Fluency
Choose the shape before you type. Then say it in one line.
Intermediate~5h7 modules
Python Confidence
Somebody else wrote it. Find out whether it works.
Advanced~7h7 modules
Python Craft
It works. Now make it somebody else's.
Advanced~10h7 modules
One goroutine is a program. Two is a design problem.
The first item is free. ~5h of focused work, at your own pace.
Advanced~5h6 modules