MCPcopy
hub / github.com/mao888/golang-guide / demo1

Function demo1

golang/go-study/go语言基础/并发/channel/通道误用示例.go:10–31  ·  view source on GitHub ↗

demo1 通道误用导致的bug

()

Source from the content-addressed store, hash-verified

8
9// demo1 通道误用导致的bug
10func demo1() {
11 wg := sync.WaitGroup{}
12
13 ch := make(chan int, 10)
14 for i := 0; i < 10; i++ {
15 ch <- i
16 }
17 close(ch)
18
19 wg.Add(3)
20 for j := 0; j < 3; j++ {
21 go func() {
22 for {
23 task := <-ch
24 // 这里假设对接收的数据执行某些操作
25 fmt.Println(task)
26 }
27 wg.Done()
28 }()
29 }
30 wg.Wait()
31}
32
33func demo01修改() {
34 wg := sync.WaitGroup{}

Callers

nothing calls this directly

Calls 3

DoneMethod · 0.80
AddMethod · 0.45
WaitMethod · 0.45

Tested by

no test coverage detected