-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexample_test.go
More file actions
43 lines (35 loc) · 829 Bytes
/
example_test.go
File metadata and controls
43 lines (35 loc) · 829 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
35
36
37
38
39
40
41
42
43
package broadcast
import "log"
// Example of a simple broadcaster sending numbers to two workers.
//
// Five messages are sent. The first worker prints all five. The second worker prints the first and then unsubscribes.
func Example() {
b := NewBroadcaster(100)
workerOne(b)
workerTwo(b)
for i := 0; i < 5; i++ {
log.Printf("Sending %v", i)
b.Submit(i)
}
b.Close()
}
func workerOne(b Broadcaster) {
ch := make(chan interface{})
b.Register(ch)
defer b.Unregister(ch)
// Dump out each message sent to the broadcaster.
go func() {
for v := range ch {
log.Printf("workerOne read %v", v)
}
}()
}
func workerTwo(b Broadcaster) {
ch := make(chan interface{})
b.Register(ch)
defer b.Unregister(ch)
defer log.Printf("workerTwo is done\n")
go func() {
log.Printf("workerTwo read %v\n", <-ch)
}()
}