-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
main.go
70 lines (58 loc) · 1.17 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
package main
import (
"fmt"
)
func main() {
in := gen()
out := make(chan int)
// FAN OUT
// Multiple functions reading from the same channel until that channel is closed
// Distribute work across multiple functions (ten goroutines) that all read from in.
fanOut(in, 10, out)
// FAN IN
// multiplex multiple channels onto a single channel
// merge the channels from c0 through c9 onto a single channel
go func() {
for v := range out {
fmt.Println(v)
}
}()
var a string
fmt.Scanln(&a)
}
func gen() <-chan int {
out := make(chan int)
go func() {
for i := 0; i < 10; i++ {
for j := 3; j < 13; j++ {
out <- j
}
}
close(out)
}()
return out
}
func fanOut(in <-chan int, n int, out chan<- int) {
for i := 0; i < n; i++ {
factorial(in, out)
}
}
func factorial(in <-chan int, out chan<- int) {
go func() {
for n := range in {
out <- fact(n)
}
}()
}
func fact(n int) int {
total := 1
for i := n; i > 0; i-- {
total *= i
}
return total
}
/*
This code was provided by my friend, Mike Van Sickle
He is an awesome programmer, an awesome Go programmer, and an awesome teacher
Check out his courses on pluralsight to learn more about Go!
*/