non-blocking channel options _ golang
Basic sends and receives on channels are blocking. However, we can use select with a default clause to implement non-blocking sends, receives, and even non-blocking multi-way selects
package main import ( "fmt" ) func main() { message := make(chan string) signals := make(chan bool) select { case msg := <-message: fmt.Println("received message", msg) default: fmt.Println("no message received") } msg := "hi" select { case message <- msg: fmt.Println("sent message", msg) default: fmt.Println("no message sent") } select { case msg := <-message: fmt.Println("received message", msg) case sig := <-signals: fmt.Println("received signal", sig) default: fmt.Println("no activity") } }
no message received
no message sent
no activity
总结 :
1:....