package main
import (
"fmt"
"time"
)
type info struct {
Ch chan int
}
func goRun1(v *info) {
time.Sleep(time.Second *3)
v.Ch <- 1
fmt.Println("goRun1我这里等待了3秒钟后打印")
}
func goRun2() {
time.Sleep(time.Second *3)
fmt.Println("goRun2我这里等待了3秒钟后打印")
}
func main() {
var ch chan int
infoStruct := &info{Ch:ch}
go goRun1(infoStruct)
go goRun2()
for {
select {
case val, ok := <-ch:
if ok {
fmt.Println("这里是返回的ch值:", val)
return
}
case <-time.After(time.Second * 5):
fmt.Println("超时结束")
return
}
}
}
为什么我的 goRun1 函数并未执行,并且ch的值为什么没有被select 获取到呢?