Zeus is a sleek and efficient dependency injection container for Go. Easily register "factories" (functions that create instances of types) and let zeus resolve those dependencies at runtime.
Why using zeus?
With a minimalist API, integrating zeus into any Go project is a breeze.
Register your dependencies and let zeus handle the rest.
Zeus detects and reports cycles in your dependencies to prevent runtime errors.
Zeus supports lifecycle hooks, allowing you to execute functions at the start and end of your application. This is especially useful for setups and teardowns, like establishing a database connection or gracefully shutting down services.
go get -u github.com/otoru/zeus
package main
import "github.com/otoru/zeus"
c := zeus.New()
c.Provide(func() int {
return 42
})
c.Provide(func(i int) string {
return fmt.Sprintf("Number: %d", i)
})
err := c.Run(func(s string) error {
fmt.Println(s) // Outputs: Number: 42
return nil
})
Zeus allows you to register hooks that run at the start and end of your application. This is useful for setting up and tearing down resources.
c := zeus.New()
// Servoce is a dummy service that depends on Hooks.
type Service struct{}
c.Provide(func(h zeus.Hooks) *Service {
h.OnStart(func() error {
fmt.Println("Starting up...")
return nil
})
h.OnStop(func() error {
fmt.Println("Shutting down...")
return nil
})
return &Service{}
})
c.Run(func(s *Service) {
fmt.Println("Main function running with the service!")
})
// Outputs:
// Starting up...
// Main function running with the service!
// Shutting down...
Zeus now supports merging two containers together using the Merge method. This is especially useful when you have modularized your application and want to combine dependencies from different modules.
- Create two separate containers.
- Add factories to both containers.
- Use the Merge method to combine the factories of one container into another.
containerA := zeus.New()
containerB := zeus.New()
containerA.Provide(func() string { return "Hello" })
containerB.Provide(func() int { return 42 })
err := containerA.Merge(containerB)
if err != nil {
// Handle merge error
}
If a factory from the merging container conflicts with an existing factory in the main container, and they are not identical, a FactoryAlreadyProvidedError
will be returned. This ensures that you don't accidentally overwrite existing dependencies.
Contributions are warmly welcomed! Please open a PR or an issue if you find any problems or have enhancement suggestions.