Skip to content

gocrumb/event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Event

Go Reference

Manage and dispatch events within your Go application. This is somewhat inspired from Laravel's simple approach to the observer pattern.

Installation

Install event using the go get command:

$ go get github.com/gocrumb/event

The package requires no additional dependencies other than Go itself.

Usage

Run go generate with a file like the following:

package main

//go:generate event -type=OrderPlaced,OrderShipped

type OrderPlaced struct {
	OrderID    int
	CustomerID int
}

type OrderShipped struct {
	OrderID int
}

Use events from everywhere within your applications:

OnOrderPlaced(func(e OrderPlaced) {
	fmt.Println("New Order")
	fmt.Println("Order ID:   ", e.OrderID)
	fmt.Println("Customer ID:", e.CustomerID)
	fmt.Println()
})

OnOrderShipped(func(e OrderShipped) {
	fmt.Println("Order Shipped")
	fmt.Println("Order ID:", e.OrderID)
	fmt.Println()
})

// From elsewhere in your application:
EmitOrderPlaced(OrderPlaced{
	OrderID:    5,
	CustomerID: 265,
})
EmitOrderShipped(OrderShipped{
	OrderID: 5,
})

Documentation

Contributing

Contributions are welcome.

License

This package is available under the BSD (3-Clause) License.

Releases

No releases published

Packages

No packages published

Languages