-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (44 loc) · 820 Bytes
/
main.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
package main
import (
"strconv"
"sync"
)
func main() {
println("Fan in........")
tasks1 := taskGenerator("First ")
tasks2 := taskGenerator("Second ")
tasks3 := taskGenerator("Third ")
out := merge(tasks1, tasks2, tasks3)
for s := range out {
println(s)
}
}
func taskGenerator(s string) chan string {
ch := make(chan string)
go func() {
defer close(ch)
for i := 0; i < 3; i++ {
ch <- s + strconv.Itoa(i)
}
}()
return ch
}
// From Samir Ajamni's excellent post on blog.golang.org
func merge(channels ...<-chan string) <-chan string {
var wg sync.WaitGroup
wg.Add(len(channels))
out := make(chan string)
for _, ch := range channels {
go func(c <-chan string) {
for s := range c {
out <- s
}
wg.Done()
}(ch)
}
go func() {
wg.Wait()
close(out)
}()
return out
}