-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathmain.go
57 lines (50 loc) · 992 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
56
57
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
func randomTime() int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(100)
}
func main() {
ch01 := make(chan struct{})
ch02 := make(chan struct{})
ch03 := make(chan struct{})
count := 10
wg := &sync.WaitGroup{}
wg.Add(count)
go func() {
ch01 <- struct{}{}
}()
go func(in, out chan struct{}) {
for i := 0; i < count; i++ {
<-in
time.Sleep(time.Duration(randomTime()) * time.Millisecond)
fmt.Println("cat")
out <- struct{}{}
}
}(ch01, ch02)
go func(in, out chan struct{}) {
for i := 0; i < count; i++ {
<-in
time.Sleep(time.Duration(randomTime()) * time.Millisecond)
fmt.Println("dog")
out <- struct{}{}
}
}(ch02, ch03)
go func(in, out chan struct{}) {
for i := 0; i < count; i++ {
<-in
time.Sleep(time.Duration(randomTime()) * time.Millisecond)
fmt.Println("bird")
if i != (count - 1) {
out <- struct{}{}
}
wg.Done()
}
}(ch03, ch01)
wg.Wait()
}