β° All patterns
Pipeline
Chain stages with channels so data flows through like an assembly line.
01
Explain it like I'm new
Real-life analogy
A car assembly line
One station fits the wheels, the next paints, the next inspects. All stations work at the same time on different cars, and each car flows down the line.
π© The problem
Reading, transforming and filtering a big stream in one loop means each step waits for the others, and the code becomes a tangled blob.
π‘ The pattern
Each stage is a function that takes an input channel and returns an output channel, running its own goroutine. Stages connect like LEGO bricks.
02
Play with it
An interactive model of the pattern. Click around, nothing can break.
π‘ Watch several numbers sit in different stages at the same time. Every stage is its own goroutine, connected by channels.
03
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.
04
When should I use it?
β Reach for it whenβ¦
- β’ETL jobs, log processing, streaming transforms, image processing chains.
π« Skip it whenβ¦
- β’Each stage is trivially cheap. Channel overhead may outweigh the benefit.
πΉ Go tip
Pass a context.Context into every stage and select on ctx.Done() so the whole pipeline can be cancelled.
π You've already seen it in
io.Pipe and Unix-style data processing tools, plus streaming ETL in Go data platforms.
05
Quick check
One question. No pressure.
Who should close a stage's output channel?
Related: Fan-out / Fan-in, Worker Pool, Iterator