-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (64 loc) · 1.57 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package nonblockingchannelops
import (
"fmt"
"sync"
)
/*
As we know, basic sends and receives on channels are blocking, however
incorporating `select` with a default calsue to implement non blocking sends,
receives and even non blocking multi-way selects is possible.
*/
func Run() {
nonBlockingReceive()
nonBlockingSend()
multiwayNonBLockingSelect()
}
func nonBlockingReceive() {
// An unbuffered channel, but reading does not block!
messages := make(chan string)
select {
case message := <-messages:
fmt.Println(message)
default:
fmt.Println("No new messages on the messages channel")
}
}
func nonBlockingSend() {
// Again an unbuffered channel, but sending does not block after 1 value.
channel := make(chan int)
// We cannot send this value in, as the channel is unbuffered and there
// is no receiver
val := 100
select {
case channel <- val:
fmt.Println("Sent value", val)
default:
fmt.Println("No messages could be sent.")
}
}
func multiwayNonBLockingSelect() {
// We can use multiple cases above the default clause to
// implement a multi way non blocking select. Here we will
// attempt non blocking receives on multiple channels.
channel := make(chan int, 1)
signals := make(chan bool, 1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
channel <- 100
signals <- true
wg.Done()
}()
wg.Wait()
for {
select {
case retrieved := <-channel:
fmt.Println("Retrieved a value", retrieved)
case sig := <-signals:
fmt.Println("Received a signal", sig)
default:
fmt.Println("No activity across both channels, exiting.")
return
}
}
}