-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.go
57 lines (44 loc) · 932 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"time"
)
func SelectExample() {
fmt.Println("--------------------------")
channel3 := make(chan string)
channel4 := make(chan string)
charChannel := make(chan string, 5)
go goRoutine3(channel3)
go goRoutine4(channel4)
go fillBufferedChannel(charChannel)
select {
case msg1 := <-channel3:
fmt.Println("Received from Channel 3:", msg1)
case msg2 := <-channel4:
fmt.Println("Received from Channel 4:", msg2)
}
var result string
for i := 0; i < 5; i++ {
select {
case char := <-charChannel:
result += char
}
}
fmt.Println("Received:", result)
fmt.Println("Hello From Select!")
}
func goRoutine3(c chan string) {
time.Sleep(4 * time.Second)
c <- "3"
}
func goRoutine4(c chan string) {
time.Sleep(4 * time.Second)
c <- "4"
}
func fillBufferedChannel(c chan string) {
chars := []string{"e", "r", "d", "e", "m"}
for _, v := range chars {
c <- v
}
close(c)
}