Pipeline and Stage Patterns
Composing multi-stage concurrent pipelines
What is a pipeline?
A concurrent pipeline is a series of stages connected by channels. Each stage receives values from upstream, transforms them, and sends results downstream, allowing stages to run concurrently.
Simple three-stage pipeline
Generate → transform → consume:
func generate(nums ...int) <-chan int {
out := make(chan int)
go func() { defer close(out); for _, n := range nums { out <- n } }()
return out
}
func square(in <-chan int) <-chan int {
out := make(chan int)
go func() { defer close(out); for n := range in { out <- n*n } }()
return out
}
for v := range square(generate(2, 3, 4)) {
fmt.Println(v) // 4, 9, 16
}All lessons in this course
- Pipeline and Stage Patterns
- errgroup for Concurrent Error Handling
- Semaphore Pattern
- Leak Detection and Cancellation