☰ All patterns
Fan-out / Fan-in
Split work across many goroutines, then merge the results into one channel.
01
Explain it like I'm new
Real-life analogy
A group project
The teacher hands chapters to several students (fan-out). Each writes a summary, and the team lead collects them into one report (fan-in).
😩 The problem
Fetching 5 pages one after another takes 5× as long. You want them in parallel, then a single stream of results.
💡 The pattern
Fan-out: start several workers reading from the same input channel. Fan-in: a merge function forwards every worker's output into one channel.
02
Walk through the code
Click a step to highlight its lines. This is a complete program you can copy and run.
Press ▶ Run to execute this program.This output was captured by running the program with Go 1.24. Copy it into your editor or the Go Playground and try changing things.
03
When should I use it?
✅ Reach for it when…
- •Parallel I/O: API calls, DB queries, file reads, then aggregating results.
🚫 Skip it when…
- •Results must stay in strict input order. You'd need to re-sort or index them.
🐹 Go tip
Results from a fan-in arrive in random order. Carry an index with each result if order matters.
🌍 You've already seen it in
Search backends querying many shards, and aggregators calling several microservices at once.
04
Quick check
One question. No pressure.
In merge, when is it safe to close `out`?
Related: Pipeline, Worker Pool