利用select为读channel设置超时
import (
"fmt"
"time"
)
func main() {
chResult := make(chan int, 1)
go func() {
time.Sleep(1 * time.Second)
chResult <- 5
fmt.Printf("Worker finished")
}()
select {
case res := <-chResult:
fmt.Printf("Got %d from worker\n", res)
case <-time.After(100 * time.Millisecond):
fmt.Printf("Timed out before worker finished\n")
}
}
----------Output----------
Timed out before worker finished最后更新于