Concurrency Patterns
Advanced · Concurrency
Composable building blocks for concurrent Go, all built from goroutines + channels. “Don’t communicate by sharing memory; share memory by communicating.”
- concpat1 — worker pool: N workers consume one jobs channel
- concpat2 — fan-in: merge many channels into one
- concpat3 — pipeline: chain stages via channels
Resources
Section titled “Resources”- Go blog: Go Concurrency Patterns: Pipelines and cancellation
- Go blog: Concurrency Patterns
- Go by Example: Worker Pools
Exercises
Section titled “Exercises”concpat1 test
Section titled “concpat1 test”concpat1 — worker pool
Show hint
A worker drains its jobs channel until it is closed:
for n := range jobs { results <- n * n}concpat2 test
Section titled “concpat2 test”concpat2 — fan-in
Show hint
One goroutine per input copies into out; a closer waits for them all:
for _, c := range chans { wg.Add(1) go func(c <-chan int) { defer wg.Done(); for v := range c { out <- v } }(c)}go func() { wg.Wait(); close(out) }()concpat3 test
Section titled “concpat3 test”concpat3 — pipeline
Show hint
Transform each value, then close out after the input is drained:
for n := range in { out <- n * 2}close(out)