How to Implement the Pipeline Pattern in Go

Implement the pipeline pattern in Go by chaining goroutines that pass data through channels to process streams concurrently.

Implement the pipeline pattern in Go by chaining goroutines that communicate via channels, where each stage processes data and passes it to the next.

func pipeline(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() {
    in := make(chan int)
    out := pipeline(in)
    go func() { in <- 1; in <- 2; close(in) }()
    for n := range out {
        fmt.Println(n)
    }
}