A simple event dispatcher made in Go.
Use go get.
$ go get github.com/lana/go-dispatcher
Then import the package into your own code:
import "github.com/lana/go-dispacher"
package main
import (
"context"
"log"
"github.com/lana/go-dispatcher"
)
type User struct {
Name string
Email string
}
var UserWasCreated dispatcher.EventType = "user-was-created"
type UserCreated struct {
User User
}
func (uc UserCreated) Type() dispatcher.EventType {
return UserWasCreated
}
func (uc UserCreated) Data() interface{} {
return uc.User
}
func OnUserCreated(ctx context.Context, e dispatcher.Event) {
log.Printf("A user was created: %v", e.Data())
}
func main() {
d := dispatcher.New()
d.On(UserWasCreated, OnUserCreated)
if err := d.Dispatch(context.Bachground(), e); err != nil {
log.Fatalf("failed to dispatch event: %v", err)
}
}
This project is released under the MIT licence. See LICENSE for more details.