```
package main
import (
"fmt"
"sync"
)
func main() {
wg := sync.WaitGroup{}
var ret = make(chan int)
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
ret <- i
wg.Done()
}(i)
}
go func() {
defer close(ret)
wg.Wait()
}()
fmt.Println("执行结束")
for v := range ret {
fmt.Println(v)
}
}
```
上段代码中
```
go func() {
defer close(ret)
wg.Wait()
}()
```
为什么不能替换成:
```
wg.Wait()
close(ret)
```
有疑问加站长微信联系(非本文作者)