// +build OMIT | |
package main | |
import "fmt" | |
// gen sends the values in nums on the returned channel, then closes it. | |
func gen(nums ...int) <-chan int { | |
out := make(chan int) | |
go func() { | |
for _, n := range nums { | |
out <- n | |
} | |
close(out) | |
}() | |
return out | |
} | |
// sq receives values from in, squares them, and sends them on the returned | |
// channel, until in is closed. Then sq closes the returned channel. | |
func sq(in <-chan int) <-chan int { | |
out := make(chan int) | |
go func() { | |
for n := range in { | |
out <- n * n | |
} | |
close(out) | |
}() | |
return out | |
} | |
func main() { | |
// Set up the pipeline. | |
c := gen(2, 3) | |
out := sq(c) | |
// Consume the output. | |
fmt.Println(<-out) // 4 | |
fmt.Println(<-out) // 9 | |
} |