Go
Beginner
1 min read
Select Statement and Channel Patterns
Example
package main
import (
"fmt"
"math/rand"
"time"
)
func slowOp(id int) <-chan string {
ch := make(chan string)
go func() {
delay := time.Duration(rand.Intn(300)) * time.Millisecond
time.Sleep(delay)
ch <- fmt.Sprintf("result from op %d (took %v)", id, delay)
}()
return ch
}
// fanIn merges multiple channels into one
func fanIn(cs ...<-chan string) <-chan string {
merged := make(chan string)
for _, c := range cs {
c := c // capture
go func() {
for v := range c {
merged <- v
}
}()
}
return merged
}
func main() {
// Select with timeout
ch := slowOp(1)
select {
case result := <-ch:
fmt.Println(result)
case <-time.After(150 * time.Millisecond):
fmt.Println("timed out waiting for op 1")
}
// Non-blocking receive with default
ready := make(chan int, 1)
select {
case v := <-ready:
fmt.Println("got:", v)
default:
fmt.Println("nothing ready, continuing")
}
// Fan-in from multiple goroutines
merged := fanIn(slowOp(2), slowOp(3), slowOp(4))
timeout := time.After(400 * time.Millisecond)
for i := 0; i < 3; i++ {
select {
case msg := <-merged:
fmt.Println(msg)
case <-timeout:
fmt.Println("overall timeout reached")
return
}
}
}