0Pricing
Go Academy · Lesson

Pipeline Stages

Chain channel stages.

Pipeline Stages is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Pipeline Stages” lesson free?

Yes — the full text of “Pipeline Stages” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Worker Pool Pattern
  2. Fan-Out Fan-In
  3. Pipeline Stages
  4. Graceful Shutdown
← Back to Go Academy