mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 20:56:02 +03:00
50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
/*
|
|
There's no formal definition of a pipeline in Go; it's just one of many kinds of concurrent programs. Informally, a pipeline is a series of stages connected by channels, where each stage is a group of goroutines running the same function. In each stage, the goroutines
|
|
|
|
receive values from upstream via inbound channels
|
|
perform some function on that data, usually producing new values
|
|
send values downstream via outbound channels
|
|
Each stage has any number of inbound and outbound channels, except the first and last stages, which have only outbound or inbound channels, respectively. The first stage is sometimes called the source or producer; the last stage, the sink or consumer.
|
|
*/
|
|
func main() {
|
|
// Set up the pipeline.
|
|
c := gen(2, 3)
|
|
out := sq(c)
|
|
|
|
// Consume the output.
|
|
fmt.Println(<-out) // 4
|
|
fmt.Println(<-out) // 9
|
|
}
|
|
|
|
//The first stage, gen, is a function that converts a list of integers to a channel that emits the integers in the list.
|
|
// The gen function starts a goroutine that sends the integers on the channel and closes the channel when all
|
|
// the values have been sent:
|
|
func gen(nums ...int) <-chan int {
|
|
out := make(chan int)
|
|
go func() {
|
|
defer close(out)
|
|
for _, n := range nums {
|
|
out <- n
|
|
}
|
|
}()
|
|
return out
|
|
}
|
|
|
|
//The second stage, sq, receives integers from a channel and returns a channel that emits the square of
|
|
// each received integer. After the inbound channel is closed and this stage has sent all the values downstream,
|
|
// it closes the outbound 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
|
|
}
|