File: example_test.go

package info (click to toggle)
golang-github-cenk-hub 1.0.0%2Bgit20160321.17.b864404b5f99-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 92 kB
  • ctags: 31
  • sloc: makefile: 2
file content (32 lines) | stat: -rw-r--r-- 483 bytes parent folder | download | duplicates (2)
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
package hub_test

import (
	"fmt"

	"github.com/cenk/hub"
)

// Different event kinds
const (
	happenedA hub.Kind = iota
	happenedB
	happenedC
)

// Our custom event type
type EventA struct {
	arg1, arg2 int
}

// Implement hub.Event interface
func (e EventA) Kind() hub.Kind { return happenedA }

func Example() {
	hub.Subscribe(happenedA, func(e hub.Event) {
		a := e.(EventA) // Cast to concrete type
		fmt.Println(a.arg1 + a.arg2)
	})

	hub.Publish(EventA{2, 3})
	// Output: 5
}