-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
main.go
114 lines (102 loc) · 2.63 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
)
func main() {
// read NOTE ONE below
c1 := incrementor("1")
c2 := incrementor("2")
// NOTE TWO
// the flow of code continues here in main
// incrementor is called
// launches goroutines
// returns channels which will eventually be closed
// by the code that is running in the goroutines
// launched by incrementor
// program flow in main continues on down vertically here
// NOTE THREE
// you're passing into "fanIn" two channels
// fanIn HAS TO pull values off of those channels
// otherwise: DEADLOCK
// fanIn will need to launch goroutines to pull those values
c := fanIn(c1, c2)
// NOTE SEVEN
// we are held up here
// pulling values off of c
// until c is closed
// and all values have been pulled from c
// at which point ...
for n := range c {
fmt.Println(n)
}
// NOTE EIGHT
// our program is done
// flow of code exits out of main
// program ends
}
func incrementor(s string) <-chan string {
c := make(chan string)
go func() {
for i := 0; i < 20; i++ {
// NOTE NINE
// use Sprint here, or Sprintln, not Sprintf
// this code is formatted for Sprintln
// Sprintf would need this
// c <- fmt.Sprintf("Process: %v, printing %v", s, i)
c <- fmt.Sprint("Process: "+s+" printing:", i)
}
// NOTE ONE
// incrementor
// every time it's called
// create a channel, put values on the channel
// ******** important ********
// have some other goroutine somewhere
// pulling values off the channel
// ***************************
// close the channel
// return that closed channel
// these "incrementor" goroutines are off and running
close(c)
}()
return c
}
// FAN IN
func fanIn(input1, input2 <-chan string) <-chan string {
c := make(chan string)
done := make(chan bool)
// NOTE FOUR
// these goroutines will pull values off the "incrementor" channels
// I'm defining the func to have a channel as a parameter and
// I'm passing the channels in as an argument
// as this is good practice to avoid different channels
// accessing the same data and creating a race condition
// not needed here, but good practice
// and good to know about
go func(x <-chan string) {
for n := range x {
c <- n
}
done <- true
}(input1)
go func(x <-chan string) {
for n := range x {
c <- n
}
done <- true
}(input2)
// NOTE FIVE
// this will signal when we're done writing values to c
go func() {
<-done
<-done
close(c)
}()
// NOTE SIX
// all of the above code
// just flows straight through
// goroutines are launched
// and even though they're not done processing
// program flow comes to here and this func returns
// the channel c
return c
}