go-pattern-examples/gomore/04_fan_in/fan_in.go

39 lines
536 B
Go
Raw Normal View History

2020-05-06 12:12:28 +03:00
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
}