1 回答

TA贡献1876条经验 获得超7个赞
为了表示诚意,这是程序重写的。
package main
import (
"log"
"sync"
"time"
)
func main() {
ch := make(chan int, 5) // capacity increased for demonstration
thresholdValue := 10
var wg sync.WaitGroup
wg.Add(1)
go func() {
readValues(ch)
wg.Done()
}()
for i := 0; i < thresholdValue; i++ {
ch <- i
}
close(ch)
log.Println("sending done.")
wg.Wait()
}
func readValues(ch chan int) {
for value := range ch {
<-time.After(time.Second) // for demonstratin purposes.
log.Println(value)
}
}
在此版本中退出,因为循环确实退出并且关闭了。readValuesformainch
换句话说,停止条件生效并触发退出序列(信号然后等待处理完成)end of input
- 1 回答
- 0 关注
- 144 浏览
添加回答
举报