Non blocking channel operations
Context
Section titled “Context”A select with a default case allows non‑blocking sends and receives. If no channel operation is ready, the default case executes immediately.
Example
Section titled “Example”Try to receive from a channel without blocking.
Code example
Section titled “Code example”package main
import "fmt"
func main() { ch := make(chan string)
select { case msg := <-ch: fmt.Println("received", msg) default: fmt.Println("no message received") }
// Non‑blocking send select { case ch <- "hello": fmt.Println("sent") default: fmt.Println("send would block") }}Output
Section titled “Output”no message receivedsend would block