concurrency.go raw
1 package example
2
3 func worker(id int, results chan<- int) {
4 results <- id * id
5 }
6
7 func fanOut() {
8 results := make(chan int)
9 for i := 0; i < 4; i++ {
10 go worker(i, results)
11 }
12 for i := 0; i < 4; i++ {
13 <-results
14 }
15 }
16
17 func background(fn func()) {
18 go fn()
19 }
20
21 func pipeline() {
22 ch := make(chan string)
23 go func() {
24 ch <- "hello"
25 }()
26 msg := <-ch
27 _ = msg
28 }
29