-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequenceboring.go
50 lines (40 loc) · 1.02 KB
/
sequenceboring.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
package main
import (
"fmt"
"math/rand"
"time"
)
type Message struct {
str string
wait chan bool
}
func main() {
c := fanIn(boring("Joe"), boring("Ann"))
for i := 0; i < 5; i++ {
msg1 := <-c; fmt.Println(msg1.str)
msg2 := <-c; fmt.Println(msg2.str)
msg1.wait <- true
msg2.wait <- true
}
fmt.Println("You're all boring; I'm leaving.")
}
func boring(msg string) <-chan Message { // Returns receive-only channel of strings.
c := make(chan Message)
waitForIt := make(chan bool) // Shared between all messages.
go func() { // We launch the goroutine from inside the function.
for i := 0; ; i++ {
c <- Message{ fmt.Sprintf("%s: %d", msg, i), waitForIt }
time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
<-waitForIt
}
}()
return c // Return the channel to the caller.
}
func fanIn(inputs ... <-chan Message) <-chan Message {
c := make(chan Message)
for i := range inputs {
input := inputs[i] // New instance of 'input' for each loop.
go func() { for { c <- <-input } }()
}
return c
}