-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
35 lines (29 loc) · 792 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
package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(1 * time.Second)
c1 <- "one"
}() // After 1 second writes "one" on c1
go func() {
time.Sleep(2 * time.Second)
c2 <- "two"
}() // After 2 seconds writes "two" on c2
// select can be combined with channels and go routines
// the first one to arrive will be selectec.
// we are using a for loop to demonstrate that, on the first loop
// c1 receives "one", and on the second loop c2 receives "two"
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received", msg1)
case msg2 := <-c2:
fmt.Println("received", msg2)
}
}
}