我有一个简单的频道示例:https: //play.golang.org/p/eLcpzXeCHms
package main
import (
"fmt"
)
func execute(trueChan chan<- bool, lowerRange int32, upperRange int32) {
for lowerRange <= upperRange {
fmt.Printf("\nhandling number %v", lowerRange)
if lowerRange%2 == 0 {
go func() {
trueChan <- true
}()
}
lowerRange++
}
close(trueChan)
}
func main() {
counter := 0
trueChan := make(chan bool)
execute(trueChan, 5, 25)
for {
if _, ok := <-trueChan; ok {
counter++
} else {
break
}
}
fmt.Printf("\n%v", counter)
}
第一个问题:我有时会收到一条错误消息...
handling number 5
handling number 6
handling number 7
handling number 8
handling number 9
handling number 10
handling number 11
handling number 12
handling number 13
handling number 14
handling number 15
handling number 16
handling number 17
handling number 18
handling number 19
handling number 20
handling number 21
handling number 22
handling number 23
handling number 24
handling number 25
0
panic: send on closed channel
第二个问题 - 我的计数器始终为 0。
有人可以给我一个提示,我做错了什么?
米琪卡哇伊
慕虎7371278
相关分类