-
Notifications
You must be signed in to change notification settings - Fork 23
/
fan_in.go
59 lines (50 loc) · 1.09 KB
/
fan_in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package fanin
import "sync"
// Merge operate a FanIn to compose different channels into one
func Merge(cs ...<-chan int) <-chan int {
var wg sync.WaitGroup
out := make(chan int, 3)
wg.Add(len(cs))
// Start an send goroutine for each input channel in cs. send
// copies values from c to out until c is closed, then calls wg.Done.
send := func(c <-chan int) {
for n := range c {
out <- n
}
wg.Done()
}
//启动多个 go routine 开始工作
for _, c := range cs {
go send(c)
}
// Start a goroutine to close out once all the send goroutines are
// done. This must start after the wg.Add call.
//关闭动作,放在发送一方,会更好
go func() {
wg.Wait()
close(out)
}()
return out
}
func generateNumbersPipeline(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
}