我想在超时时停止执行 goroutine。但它似乎对我不起作用。我正在使用iris
框架。
type Response struct {
data interface{}
status bool
}
func (s *CicService) Find() (interface{}, bool) {
ch := make(chan Response, 1)
go func() {
time.Sleep(10 * time.Second)
fmt.Println("test")
fmt.Println("test1")
ch <- Response{data: "data", status: true}
}()
select {
case <-ch:
fmt.Println("Read from ch")
res := <-ch
return res.data, res.status
case <-time.After(50 * time.Millisecond):
return "Timed out", false
}
}
输出:
Timed out
test
test1
预期输出:
Timed out
有人可以指出这里缺少什么吗?它确实超时但仍然运行 goroutine 来打印test
和test1
. 我只想在超时后立即停止执行 goroutine。