GoAdvanced~5h

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

01

Goroutines and channels

Launch, join, hand values over, and close without panicking.

02

select and context

Wait on several at once; cancel a whole tree on a deadline.

03

The sync package

Guard what stays put, and know when a channel was wrong.

04

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. 1

    Start it, then wait for it

    ~59 min

    Launch 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. 2

    Hand a value over

    ~51 min

    Predict 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. 3

    Wait on several at once

    ~41 min

    Read 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. 4

    Stop work nobody wants

    ~43 min

    Cancel 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. 5

    When sharing is simpler

    ~51 min

    Guard 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. 6

    Prove it

    ~1h

    Find 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

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