Concept Flow - Default case in select
Start select
Check channels ready?
No→Default case runs
Yes
Run ready channel case
End select
The select statement checks if any channel is ready. If none are ready, it runs the default case immediately.
select {
case msg := <-ch1:
fmt.Println("Received", msg)
default:
fmt.Println("No message received")
}| Step | Channel ch1 ready? | Action | Output |
|---|---|---|---|
| 1 | No | Run default case | No message received |
| 2 | Yes | Receive from ch1 | Received <message> |
| Variable | Start | After Step 1 | After Step 2 |
|---|---|---|---|
| msg | undefined | undefined | <message from ch1> |
select {
case <-ch1:
// runs if ch1 ready
default:
// runs if no channels ready immediately
}
Default case prevents blocking by running instantly if no channel is ready.