Concurrency
Goroutines are cheap, and a program that starts them owns their lifetime. These lessons cover the scheduler, channels buffered and unbuffered, select, the sync package and its atomics, context for cancellation, and the patterns built on them: worker pools, pipelines, fan-in and fan-out, rate limits. The part ends on the races, deadlocks and leaks a run can hide.
6.1
Goroutines
go f(), a goroutine that finishes after main, time.Sleep and the reason a program needs a wait
Open
6.2
The scheduler, GOMAXPROCS and the runtime
G, M and P, GOMAXPROCS, a blocked goroutine and what the runtime does next
Open
6.3
Channels
an unbuffered channel, send and receive, close, a range over a channel and the zero value of a channel
Open
6.4
Buffered channels and closing
make(chan T, n), len and cap, close, ranging until closed and the send on a closed channel panic
Open
6.5
select, timeouts and default
select over two channels, time.After, a default branch and a done channel
Open
6.6
sync.Mutex and shared state
Mutex, Lock and Unlock, RWMutex, a counter that stops losing updates and the -race report
Open
6.7
sync.WaitGroup and structured waits
Add, Done and Wait, a worker per item, errgroup in words and a wait that cannot be forgotten
Open
6.8
sync.Once, sync.Pool and sync.Map
Once for a lazy value, Pool for buffers, Map for a concurrent cache and when not to use each
Open
6.9
sync/atomic and lock-free counters
atomic.Int64, Add, Load, CompareAndSwap and the counter without a mutex
Open
6.10
Context: cancellation and deadlines
context.Background, WithCancel, WithTimeout, Done and the error a cancelled call returns
Open
6.11
Context values and where they stop
WithValue, a request id, the rule that a value is not a parameter and why keys are typed
Open
6.12
Worker pools
a jobs channel, a fixed number of workers, a results channel and a pool that closes cleanly
Open
6.13
Pipelines
a stage as a function, a channel between stages, cancellation and a pipeline that drains
Open
6.14
Fan-out, fan-in and ordering
fan-out to several workers, fan-in with a merge, and an order kept by an index
Open
6.15
Rate limiting and backpressure
time.Ticker as a limiter, a semaphore channel, a bounded queue and a client that slows down
Open
6.16
Races, deadlocks and leaks
a data race found by -race, a deadlock from two locks, a leaked goroutine and a stack dump
Open