我正在尝试创建一个命令行测验,在该测验中,用户将被问到另一个问题,直到他们完成最后一个问题或超时为止。
我想使用频道,以便可以学习如何正确使用它们,可以说,我遇到了障碍。
这个想法是将correctAnswersCh从0开始,并且在每个正确答案之后,它将增加1。
在我将零放入通道后,测验总是在quiz()函数的第3行停止。
我在下面添加了一些代码,但是完整的代码在这里:https://play.golang.org/p/vzRCTc7MpIK
func main() {
questions, err := getCsvData()
var limit = time.Duration(3)
flag.Parse()
if err != nil {
log.Fatal(err)
}
quizComplete := make(chan bool)
correctAnswersCh := make(chan int)
go quiz(quizComplete, questions, correctAnswersCh)
select {
case <-time.After(limit*time.Second):
fmt.Println("Timed Out")
}
fmt.Printf("Correct Answers: %v\n", <-correctAnswersCh)
}
func quiz(quizComplete chan bool, questions [][]string, correctAnswersCh chan int) {
reader := bufio.NewReader(os.Stdin)
correctAnswersCh <- 0
// execution stops here. 0 is added to correctAnswersCh, then the quiz func stops
for _, question := range questions {
fmt.Print(question[0], "= ")
answer, _ := reader.ReadString('\n')
if strings.TrimSpace(answer) == question[1] {
cA := <-correctAnswersCh
cA++
correctAnswersCh <- cA
}
}
quizComplete <- true
}
您的correctAnswersCh
通道是未缓冲的,因此在该通道上发送任何内容都会被阻止,直到有人从该通道接收到为止。而且由于main()
函数仅在超时后才从中接收,因此直到那时您的应用程序都被阻止。
一个简单的解决方法是为通道分配1个缓冲区:
correctAnswersCh := make(chan int, 1)
尽管这是一些奇怪的渠道用法。如果您打算创建并发安全计数器,请使用原子计数器,例如atomic.AddInt32()
。另一种选择是当从多个goroutine同时访问时,使用互斥锁(atomic.AddInt32()
或sync.Mutex
)保护资源(变量)。