go · level 3 · unit 5 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A joins stages with channels, like a factory line.
A pipeline is a chain of stages joined by channels. Each is a goroutine that receives from the channel before it, does one small job and sends the result on. Like a factory line, every stage can be busy at the same time with a different item.
The rule that keeps it tidy: each stage ranges over its input and closes its output when the input runs dry. The close then flows down the line, stage by stage, until the last loop in main ends.
A parameter can hold a function, like f func(string) string below. So one stage can do any job you hand it, such as strings.TrimSpace or strings.ToUpper.
package main
import (
"fmt"
"strings"
)
func stage(in <-chan string, f func(string) string) <-chan string {
out := make(chan string)
go func() {
defer close(out)
for s := range in {
out <- f(s)
}
}()
return out
}
func main() {
src := make(chan string, 2)
src <- " tea "
src <- " cake"
close(src)
for s := range stage(stage(src, strings.TrimSpace), strings.ToUpper) {
fmt.Println(s)
}
} defer close(out) closes the output however the goroutine ends.
guess first
Every value goes through double twice. What prints?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
package main
import "fmt"
func double(in <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for n := range in {
out <- n * 2
}
}()
return out
}
func main() {
src := make(chan int, 3)
src <- 1
src <- 2
src <- 3
close(src)
for n := range double(double(src)) {
fmt.Print(n, " ")
}
}
your turn
Your turn. Tidy up the shouting.
Type two words with spaces around them. Then choose the second stage: ToUpper or ToLower. The brackets show that the spaces were trimmed.
output
press run to see what happens
fix it
The line stops after the last jar. Can you fix it?
output
press run to see what happens
- Both jars come out, then everything waits. What tells the next stage that no more values are coming?
srcis closed, but neither stage ever closes its own output, so therangeloops after it never end.- Add
defer close(out)as the first line inside the goroutine instage.
nailed it
your data flows down a production line. :D
- Each stage receives, does one job, and sends on.
- Every stage closes its output when its input runs dry.
- Stages work at the same time on different items.
new words in your collection
- pipeline
- stage
This lesson isn’t saved. A free account remembers every lesson you finish.