-
Notifications
You must be signed in to change notification settings - Fork 238
/
main.go
64 lines (55 loc) · 1.5 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
package main
import (
"fmt"
"math/rand"
"time"
)
// the boring function return a channel to communicate with it.
func boring(msg string) <-chan string { // <-chan string means receives-only channel of string.
c := make(chan string)
go func() { // we launch goroutine inside a function.
for i := 0; ; i++ {
c <- fmt.Sprintf("%s %d", msg, i)
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
}
}()
return c // return a channel to caller.
}
// <-chan string only get the receive value
// fanIn spawns 2 goroutines to reads the value from 2 channels
// then it sends to value to result channel( `c` channel)
func fanIn(c1, c2 <-chan string) <-chan string {
c := make(chan string)
go func() {
for { // infinite loop to read value from channel.
v1 := <-c1 // read value from c2. This line will wait when receiving value.
c <- v1
}
}()
go func() {
for {
c <- <-c2 // read value from c2 and send it to c
}
}()
return c
}
func fanInSimple(cs ...<-chan string) <-chan string {
c := make(chan string)
for _, ci := range cs { // spawn channel based on the number of input channel
go func(cv <-chan string) { // cv is a channel value
for {
c <- <-cv
}
}(ci) // send each channel to
}
return c
}
func main() {
// merge 2 channels into 1 channel
// c := fanIn(boring("Joe"), boring("Ahn"))
c := fanInSimple(boring("Joe"), boring("Ahn"))
for i := 0; i < 5; i++ {
fmt.Println(<-c) // now we can read from 1 channel
}
fmt.Println("You're both boring. I'm leaving")
}