The github.com/lltpkg/event
package provides a simple and thread-safe mechanism for managing events and listeners in Go applications.
To use the event
package in your Go project, you can use the following go get
command:
go get -u github.com/lltpkg/event
package main
import (
"fmt"
"github.com/lltpkg/event"
)
func main() {
evName := "Greeting"
go func() {
data := "World"
event.FireEvent(evName, data)
}()
evChan, unSub := event.EventChannel(evName)
defer unSub()
receivedData := <-evChan
fmt.Println("Hello,", receivedData)
}
The package allows you to create named events and associate listeners with them. Use the EventChannel function to create an event channel and associate a cleanup function with it:
// Create an event channel(listener) for "exampleEvent"
eventChan, cleanup := event.EventChannel("exampleEvent")
// Cleanup resources when done
defer cleanup()
You can trigger events using the FireEvent function. This function allows you to send data to all registered listeners for a specific event:
// Trigger the event anywhere else
event.FireEvent("exampleEvent", "event data")
Feel free to open issues and submit pull requests for improvements or bug fixes. We appreciate any contributions that make the event package more robust and versatile.
1. Kai - @lyluongthienThis package is licensed under the MIT License.
Note: Update the documentation based on the actual functionality and features provided by the
event
package. Include details about how to use the package, any configuration options, and examples for common use cases.