https://github.com/kitstack/eventkit
Eventkit package provides event-driven programming in Go for managing and triggering events with ease.
https://github.com/kitstack/eventkit
event golang
Last synced: 2 months ago
JSON representation
Eventkit package provides event-driven programming in Go for managing and triggering events with ease.
- Host: GitHub
- URL: https://github.com/kitstack/eventkit
- Owner: kitstack
- License: mit
- Created: 2023-05-10T03:09:55.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-19T21:11:52.000Z (about 3 years ago)
- Last Synced: 2024-05-02T23:35:57.984Z (about 2 years ago)
- Topics: event, golang
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/kitstack/eventkit/actions/workflows/coverage.yml)

[](https://goreportcard.com/report/github.com/kitstack/eventkit)
[](https://codecov.io/gh/kitstack/eventkit)
[](https://github.com/kitstack/eventkit/blob/main/LICENSE)
[](https://github.com/kitstack/eventkit/releases)
# Overview
The `eventkit` package provides a simple event-driven programming mechanism in Go. It allows you to create and manage events, subscribe to events using struct methods or functions, and trigger events with optional data.
## Usage
### Creating a new event instance
To create a new event instance, use the `New()` function:
```go
eventInstance := eventkit.New()
```
### Subscribing to an event
#### Using struct methods
To subscribe to an event using struct methods, use the `Subscribe()` function. It takes a struct instance as the payload and automatically registers all methods starting with a specified prefix (default: "On") as event listeners.
```go
type MyEventPayload struct {
// struct fields
}
func (m *MyEventPayload) OnEventName() {
// event handler logic
}
// Subscribe to the event
eventInstance.Subscribe(&MyEventPayload{})
```
#### Using functions
To subscribe to an event using a function, use the `SubscribeFunc()` function. Provide a unique listener identifier and the callback function.
```go
func myEventListener(data ...interface{}) {
// event handler logic
}
// Subscribe to the event
eventInstance.SubscribeFunc("myListener", myEventListener)
```
### Triggering an event
To trigger an event, use the `Trigger()` function. Provide the name of the event and optional data as arguments.
```go
err := eventInstance.Trigger("eventName", eventData)
if err != nil {
// handle error
}
```
Note: The package utilizes various external dependencies, such as `logrus`, `golang.org/x/text/cases`, and `sync`, among others.
For more details on the package, please refer to the code documentation and comments within the source files.