package main
import (
"fmt"
"sync"
)
func Producer(msg chan int, wg *sync.WaitGroup) {
go func() {
for i := 0; i < 10; i++ {
msg <- i
}
close(msg)
wg.Done()
}()
}
func Consumer(msg chan int, wg *sync.WaitGroup) {
go func() {
for i := range msg {
fmt.Println(i)
}
wg.Done()
}()
}
func main() {
msg := make(chan int)
wg := sync.WaitGroup{}
wg.Add(1)
Producer(msg, &wg)
wg.Add(1)
Consumer(msg, &wg)
wg.Add(1)
Consumer(msg, &wg)
wg.Wait()
}
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
goroutine 记录错误(errgroup只记录第一个错误,内部使用once)
package main
import (
"context"
"crypto/md5"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"golang.org/x/sync/errgroup"
)
// Pipeline demonstrates the use of a Group to implement a multi-stage
// pipeline: a