forked from lotusirous/go-concurrency-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (65 loc) · 2.13 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
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"math/rand"
"time"
)
type Message struct {
str string
wait chan bool
}
func fanIn(inputs ...<-chan Message) <-chan Message {
c := make(chan Message)
for i := range inputs {
input := inputs[i]
go func() {
for {
c <- <-input
}
}()
}
return c
}
// the boring function return a channel to communicate with it.
func boring(msg string) <-chan Message { // <-chan string means receives-only channel of string.
c := make(chan Message)
waitForIt := make(chan bool) // share between all messages
go func() { // we launch goroutine inside a function.
for i := 0; ; i++ {
c <- Message{
str: fmt.Sprintf("%s %d", msg, i),
wait: waitForIt,
}
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
// every time the goroutine send message.
// This code waits until the value to be received.
<-waitForIt
}
}()
return c // return a channel to caller.
}
func main() {
// merge 2 channels into 1 channel
c := fanIn(boring("Joe"), boring("Ahn"))
for i := 0; i < 5; i++ {
msg1 := <-c // wait to receive message
fmt.Println(msg1.str)
msg2 := <-c
fmt.Println(msg2.str)
// each go routine have to wait
msg1.wait <- true // main goroutine allows the boring goroutine to send next value to message channel.
msg2.wait <- true
}
fmt.Println("You're both boring. I'm leaving")
}
// main: goroutine boring: goroutine
// | |
// | |
// wait for receiving msg from channel c c <- Message{} // send message
// <-c |
// | |
// | <-waitForIt // wait for wake up signal
// send value to channel |
// hey, boring. You can send next value to me |
// wait <-true |
/// REPEAT THE PROCESS