```golang
package main
import (
"fmt"
"sync"
)
func main() {
wg:=&sync.WaitGroup{}
ch:=make(chan int ,100)
send:= func() {
for i:=0;i<100;i++{
ch<-i
}
//不开启close,如何不死锁?
//close(ch)
}
recv:= func(id int) {
defer wg.Done()
for i:=range ch{
fmt.Printf("cusumerV1 #%d get %d\n", id, i)
}
fmt.Printf(" >>>>>>consumerV1 #%d exit,\n", id)
for{
if i,ok := <-ch;ok{
fmt.Printf("cusumerV2 #%d get %d,closed is %v\n", id, i,ok)
}else{
fmt.Printf(" >>>>>>consumerV2 #%d exit,closed is %v\n", id,ok)
return
}
}
}
wg.Add(3)
go recv(0)
go recv(1)
go recv(2)
go send()
wg.Wait()
fmt.Println("all done")
}
```
对close的行为很迷惑,只是一个标记位,为啥不close会死锁呢。。
有疑问加站长微信联系(非本文作者))
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)