-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27_select.go
More file actions
38 lines (33 loc) · 761 Bytes
/
27_select.go
File metadata and controls
38 lines (33 loc) · 761 Bytes
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
package gobyexample
import (
"fmt"
"time"
)
// SelectDemo - demonstrates `select` waiting on multiple
// channel operations
func SelectDemo() {
// select across two channels.
// each channel will receive a value after some amount of time,
// to simulate e.g. blocking RPC operations executing in
// concurrent goroutines
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(1 * time.Second)
c1 <- "one"
}()
go func() {
time.Sleep(2 * time.Second)
c2 <- "two"
}()
// 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)
}
}
}