Pipeline Stages
Chain channel stages.
Pipeline Stages is a free Go Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
What Is a Pipeline
A pipeline is a series of stages connected by channels. Each stage receives values, does work, and sends results downstream. Data flows like an assembly line.
- Each stage is a goroutine
- Stages communicate only through channels
- Composable and easy to reason about
Stage Signature Convention
By convention each stage takes one or more receive-only input channels and returns a receive-only output channel. This makes stages chainable.
func stage(in <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for v := range in {
out <- v
}
}()
return out
}The Source Stage
The first stage produces data. It often takes a slice or variadic arguments and streams them into a channel.
func numbers(vals ...int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for _, v := range vals {
out <- v
}
}()
return out
}A Transform Stage
This stage multiplies every value by ten. It reads from in and writes the transformed value to out.
func tenfold(in <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for v := range in {
out <- v * 10
}
}()
return out
}A Filter Stage
A filter only forwards values that meet a condition. Here we keep even numbers and drop the rest.
func evens(in <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for v := range in {
if v%2 == 0 {
out <- v
}
}
}()
return out
}Chaining Stages
Compose stages by passing one output as the next input. The chain reads almost like a sentence.
pipe := tenfold(evens(numbers(1, 2, 3, 4)))
for v := range pipe {
fmt.Println(v)
}defer close(out)
Using defer close(out) guarantees the output channel closes when the stage goroutine returns, even if you add early returns later. This propagates the end-of-stream signal downstream.
A Complete Pipeline
Source then filter then transform. Run it to see only the tenfold of even inputs.
package main
import "fmt"
func numbers(vals ...int) <-chan int {
out := make(chan int)
go func() { defer close(out); for _, v := range vals { out <- v } }()
return out
}
func evens(in <-chan int) <-chan int {
out := make(chan int)
go func() { defer close(out); for v := range in { if v%2 == 0 { out <- v } } }()
return out
}
func tenfold(in <-chan int) <-chan int {
out := make(chan int)
go func() { defer close(out); for v := range in { out <- v * 10 } }()
return out
}
func main() {
for v := range tenfold(evens(numbers(1, 2, 3, 4, 5, 6))) {
fmt.Println(v)
}
}Streaming, Not Batching
Values flow through the pipeline one at a time as they are produced. The transform stage can start working on the first item before the source has emitted the last. This keeps memory low and latency small.
Pipelines Are Lazy
A stage does no work until something reads from its output. If the consumer stops ranging, upstream stages block on their sends. This natural backpressure prevents wasted computation.
Adding Concurrency
To speed a slow stage, fan it out into multiple instances reading the same input and merge their outputs. The stage signature stays the same, only the wiring changes.
Quick Check
Test your pipeline knowledge.
Recap
You learned pipeline stages:
- Each stage: receive-only input, returns receive-only output
- defer close(out) propagates end-of-stream
- Chain stages by nesting calls
- Pipelines stream lazily with backpressure
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Pipeline Stages” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Pipeline Stages”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
What will I learn in “Pipeline Stages”?
Chain channel stages. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Pipeline Stages” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Worker Pool Pattern
- Fan-Out Fan-In
- Pipeline Stages
- Graceful Shutdown