-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.go
More file actions
34 lines (27 loc) · 690 Bytes
/
select.go
File metadata and controls
34 lines (27 loc) · 690 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
package main
import (
"fmt"
// "time"
)
func SelectProgram() {
messages := make(chan string)
signals := make(chan bool)
// fmt.Println(<-messages)
// fmt.Println(<-signals) // result in deadlock as both are unbuffered so blocking in nature
/* Try uncommenting this
go func(){
messages<-"hello";
}()
time.Sleep(1 * time.Second)
*/
select{
case msg := <-messages:
fmt.Println("Received msg: ", msg)
case signal := <-signals:
fmt.Println("Received msg: ", signal)
default:
fmt.Println("No message received")
} // here select will chose the one which executes first here as both have nothing to receive the default is executed
close(messages)
close(signals)
}