-
Notifications
You must be signed in to change notification settings - Fork 26
/
select.go
43 lines (34 loc) · 1000 Bytes
/
select.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
38
39
40
41
42
43
package main
import (
"fmt"
"time"
)
// Go’s select lets you wait on multiple channel operations.
// Combining goroutines and channels with select is a powerful feature of Go.
// Select func illustration Go's Select
func Select() {
// For our example we’ll select across two channels.
c1 := make(chan string)
c2 := make(chan string)
// Each channel will receive a value after some amount of time,
// to simulate e.g. blocking RPC operations executing in concurrent goroutines.
go func() {
time.Sleep(1 * time.Second)
c1 <- "One"
}()
go func() {
time.Sleep(2 * time.Second)
c2 <- "Two"
}()
// We’ll use select to await both of these values simultaneously,
// printing each one as it arrives.
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received ", msg1)
case msg2 := <-c2:
fmt.Println("received ", msg2)
}
}
// Note that the total execution time is only ~2 seconds since both the 1 and 2 second Sleeps execute concurrently.
}