Skip to content

Non blocking channel operations

A select with a default case allows non‑blocking sends and receives. If no channel operation is ready, the default case executes immediately.

Try to receive from a channel without blocking.

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")
}
}
Terminal window
no message received
send would block