Package eventbus is an async bus with batteries included for Golang.
Make sure that Go is installed on your computer. Type the following command in your terminal:
go get github.com/tibrn/eventbus
After it the package is ready to use.
Add following line in your *.go
file:
import "github.com/tibrn/eventbus"
func add(a int, b int) {
fmt.Printf("%d\n", a + b)
}
func main() {
bus := eventbus.New();
bus.On("add", add);
bus.Emit("add", 20, 40);
}
- New()
- On()
- Emit()
New returns new EventBus with empty handlers.
bus := eventbus.New();
Subscribe to event. Returns error if fn
is not a function.
func Handler() { ... }
...
removeHandler, err := bus.On("event:handler", Handler)
Emit executes callback defined for an event. Any additional argument will be transferred to the callback.
arg1, arg2 := 4,5
...
waitEmit := bus.Emit("event:handler", arg1, arg2)