问题遇到的现象和发生背景
Go语言通道问题
package main
import (
"fmt"
"time"
)
func sender(c chan string) {
t := time.NewTicker(1 * time.Second)
for {
c <- "I am sending a message"
<-t.C
}
}
func main() {
massage := make(chan string)
stop := make(chan bool)
go sender(massage)
go func() {
time.NewTicker(2 * time.Second)
fmt.Println("Time is up!")
stop <- true
}()
for {
select {
case <-stop:
fmt.Println("退出")
return
case msg := <-massage:
fmt.Println(msg)
}
}
}
用代码块功能插入代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
“time is up”最先输出说明已经对stop传递true了那为什么程序没有结束