Debian Codegraph @

examples/basic.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5
6 "github.com/wagoodman/go-partybus"
7 )
8
9 type A struct {
10 a string
11 b int
12 }
13
14 type B struct {
15 c int64
16 d float64
17 }
18
19 func (b *B) Number() float64 {
20 return b.d
21 }
22
23 type Number interface {
24 Number() float64
25 }
26
27 func generate(bus *partybus.Bus) {
28 bus.Publish(partybus.Event{
29 Type: "a",
30 Value: A{"yeah", 4},
31 })
32 bus.Publish(partybus.Event{
33 Type: "b",
34 Value: B{42, 5.5},
35 })
36 bus.Publish(partybus.Event{
37 Type: "n",
38 Value: Number(&B{42, 5.5}),
39 })
40 bus.Close()
41 }
42
43 func main() {
44
45 bus := partybus.NewBus()
46 subscription := bus.Subscribe()
47
48 go generate(bus)
49
50 for event := range subscription.Events() {
51 fmt.Printf("Event: %+v\n", event)
52 switch event.Value.(type) {
53 case string:
54 fmt.Println("STRING", event.Value.(string))
55 case Number:
56 fmt.Println("Number", event.Value.(Number).Number())
57 case A:
58 fmt.Println("A type", event.Value.(A).a)
59 case B:
60 fmt.Println("B type", event.Value.(B).c)
61 default:
62 fmt.Println("blerg...")
63 }
64 }
65
66 }