mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-25 21:26:03 +03:00
39 lines
536 B
Go
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
|
||
|
}
|