Package EventBus is the little and lightweight eventbus for GoLang.
Make sure that Go is installed on your computer. Type the following command in your terminal:
go get github.com/asaskevich/EventBus
After it the package is ready to use.
Add following line in your *.go
file:
import "github.com/asaskevich/EventBus"
If you unhappy to use long EventBus
, you can do something like this:
import (
evbus "github.com/asaskevich/EventBus"
)
func calculator(a int, b int) {
fmt.Printf("%d\n", a + b)
}
func main() {
bus := EventBus.New();
bus.Subscribe("main:calculator", calculator);
bus.Publish("main:calculator", 20, 40);
bus.Unsubscribe("main:calculator");
}
- New()
- Subscribe()
- SubscribeOnce()
- Unsubscribe()
- Publish()
New returns new EventBus with empty handlers.
bus := EventBus.New();
Subscribe to a channel.
func Handler() { ... }
...
bus.Subscribe("channel:handler", Handler)
Subscribe to a channel once. Handler will be removed after executing.
func HelloWorld() { ... }
...
bus.SubscribeOnce("channel:handler", HelloWorld)
Remove callback defined for a channel.
bus.Unsubscribe("channel:handler");
Execute callback defined for a channel. Any addional argument will be tranfered to the callback.
func Handler(str string) { ... }
...
bus.Subscribe("channel:handler", Handler)
...
bus.Publish("channel:handler", "Hello, World!");
If you do have a contribution for the package feel free to put up a Pull Request or open Issue.