-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (59 loc) · 1.17 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"sync"
"time"
)
// main demonstrates the fan in pattern.
// consolidating data from multiple goroutines.
func main() {
// invoke a long running io function, three times.
a, b, c := someIO(20), someIO(20), someIO(20)
fanned := fanIn(a, b, c)
for element := range fanned {
fmt.Println(element)
}
}
// fanIn demonstartes a simple way to fan in multiple channels
// into one.
func fanIn(ch ...<-chan status) <-chan status {
out := make(chan status)
var wg sync.WaitGroup
wg.Add(len(ch))
for _, channel := range ch {
go func(c <-chan status) {
defer wg.Done()
for v := range c {
out <- v
}
}(channel)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
// status encapsulates some response from a server
type status struct {
code int
message string
}
// someIO simulates a long running function.
func someIO(size int) <-chan status {
c := make(chan status, size)
var wg sync.WaitGroup
wg.Add(size)
for i := range size {
go func(i int) {
defer wg.Done()
time.Sleep(200 * time.Millisecond)
c <- status{code: i, message: fmt.Sprintf("message %d", i)}
}(i)
}
go func() {
wg.Wait()
close(c)
}()
return c
}