Concept Flow - Select with multiple channels
Start
Wait for any channel
Chan1
Handle
Repeat or Exit
The program waits for messages from multiple channels and handles whichever is ready first, including a default case if none are ready.
package main import "fmt" func main() { c1 := make(chan string) c2 := make(chan string) select { case msg1 := <-c1: fmt.Println("Received", msg1) case msg2 := <-c2: fmt.Println("Received", msg2) default: fmt.Println("No message received") } }
| Step | Select Case | Channel Ready? | Action Taken | Output |
|---|---|---|---|---|
| 1 | case msg1 := <-c1 | No | Skipped | |
| 2 | case msg2 := <-c2 | No | Skipped | |
| 3 | default | N/A | Executed | No message received |
| 4 | End | N/A | Program ends |
| Variable | Start | After Select |
|---|---|---|
| msg1 | undefined | undefined |
| msg2 | undefined | undefined |
select {
case <-chan1:
// handle chan1
case <-chan2:
// handle chan2
default:
// handle no ready channel
}
- Waits for multiple channels
- Executes first ready case
- Default runs if none ready
- Useful for concurrent waits