-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
49 lines (41 loc) · 819 Bytes
/
example_test.go
File metadata and controls
49 lines (41 loc) · 819 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
44
45
46
47
48
49
package topic_test
import (
"context"
"errors"
"fmt"
"github.com/ostcar/topic"
)
func ExampleTopic() {
ctx, shutdown := context.WithCancel(context.Background())
top := topic.New[string]()
// Write the messages v1, v2 and v3 in a different goroutine.
go func() {
top.Publish("v1")
top.Publish("v2", "v3")
shutdown()
}()
// Receive the two messages and print all values.
var id uint64
var values []string
var err error
for {
id, values, err = top.ReceiveSince(ctx, id)
if err != nil {
if errors.Is(err, context.Canceled) {
// shutdown was called.
return
}
// Handle Error:
fmt.Printf("Receive() returned an unexpected error: %v", err)
return
}
// Process values:
for _, v := range values {
fmt.Println(v)
}
}
// Unordered output:
// v1
// v2
// v3
}