使用Select實現無阻塞讀取
go func() {
select {
case eventChan <- value:
fmt.Println("add chan done")
default:
fmt.Println("default:", value)// receiving from c would block
}
}()go func() {
timeout := time.NewTimer(time.Microsecond * 500)
select {
case eventChan <- value:
fmt.Println("add chan done")
case <-timeout.C:
fmt.Println("write time out")
}
}()Last updated