go-pattern-examples/gomore/04_fan_in/fan_in.go
2020-05-06 17:12:28 +08:00

39 lines
536 B
Go

package fanin
func generatePipeline(numbers []int) <-chan int {
out := make(chan int)
go func() {
for _, n := range numbers {
out <- n
}
close(out)
}()
return out
}
func squareNumber(in <-chan int) <-chan int {
out := make(chan int)
go func() {
for n := range in {
out <- n * n
}
close(out)
}()
return out
}
func fanIn(input1, input2 <-chan int) <-chan int {
c := make(chan int)
go func() {
for {
select {
case s := <-input1:
c <- s
case s := <-input2:
c <- s
}
}
}()
return c
}