capypad
0 day streak
go / beginner
Snippet

Channel Communication with Select

The select statement lets a goroutine wait on multiple channel operations simultaneously. It blocks until one of its cases can proceed, then executes that case. If multiple cases are ready, select chooses randomly. This is Go's way of handling asynchronous communication between goroutines. The select with a default case is non-blocking - it checks all cases without waiting.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
 
import (
"fmt"
"time"
)
 
func worker(id int, ch chan string, done chan bool) {
for i := 0; i < 3; i++ {
select {
case msg := <-ch:
fmt.Printf("Worker %d received: %s\n", id, msg)
case <-time.After(time.Millisecond * 100):
fmt.Printf("Worker %d timed out\n", id)
}
}
done <- true
}
 
func main() {
ch1 := make(chan string, 1)
ch2 := make(chan string, 1)
done := make(chan bool)
 
go worker(1, ch1, done)
go worker(2, ch2, done)
 
ch1 <- "Hello from main"
ch2 <- "Greetings from main"
 
time.Sleep(time.Millisecond * 50)
ch1 <- "Another message to worker 1"
 
<-done
<-done
fmt.Println("All workers finished")
}
Breakdown
1
select { case msg := <-ch: }
Wait for a value to be sent on ch, then assign to msg and execute the case
2
case <-time.After(time.Millisecond * 100)
Built-in timeout case - receives from a channel that sends after the duration
3
ch1 := make(chan string, 1)
Buffered channel with capacity 1 - sender won't block until buffer is full
4
go worker(1, ch1, done)
Starts worker goroutine - runs concurrently with main function