Skip to content

kodeyeen/event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

event

A Go event management package.

Installation

go get github.com/kodeyeen/event

Quickstart

package main

import (
	"context"
	"fmt"

	"github.com/kodeyeen/event"
)

// Declare event payload.
// It's ok to name it with Event suffix though it's not the event itself, but its payload.
type UserRegisteredEvent struct {
	Username string
}

func main() {
	// Create an event dispatcher.
	dispr := event.NewDispatcher()

	// Register an event listener.
	dispr.ListenFunc("user.registered", func(ctx context.Context, e event.Event) error {
		p := e.Payload().(*UserRegisteredEvent)

		fmt.Printf("New user: %s\n", p.Username)

		return nil
	})

	// Create an event to be dispatched
	userRegisteredEvt := event.WithPayload("user.registered", &UserRegisteredEvent{
		Username: "kodeyeen",
	})

	// Dispatch the event.
	dispr.HandleEvent(context.Background(), userRegisteredEvt)
}

About

Simple idiomatic Golang event dispatching library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages