Skip to content

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

concpat1 — worker pool

Show hint
A worker drains its jobs channel until it is closed:
for n := range jobs {
results <- n * n
}

Source

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) }()

Source

concpat3 — pipeline

Show hint
Transform each value, then close out after the input is drained:
for n := range in {
out <- n * 2
}
close(out)

Source