-
Notifications
You must be signed in to change notification settings - Fork 2
/
select.go
53 lines (44 loc) · 1.22 KB
/
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
package main
import (
"fmt"
"time"
)
/*******************************************************************************
The functions sendToC1 and sendToC2 infinitely send "From 1", and "From 2"
to channel, sleeping a second each time.
********************************************************************************/
func sendToC1(channel chan string) {
for {
channel <- "From 1"
time.Sleep(time.Second)
}
}
func sendToC2(channel chan string) {
for {
channel <- "From 2"
time.Sleep(time.Second)
}
}
/*******************************************************************************
This function takes 2 channels, and has a select statement, choosing to Print
the value from each channel as soon as it's ready.
********************************************************************************/
func selectChannels(c1 chan string, c2 chan string) {
for {
select {
case msg1 := <- c1:
fmt.Println(msg1)
case msg2 := <- c2:
fmt.Println(msg2)
}
}
}
func main() {
c1 := make(chan string)
c2 := make(chan string)
go sendToC1(c1);
go sendToC2(c2);
go selectChannels(c1,c2);
var input string
fmt.Scanln(&input)
}