Documentation
¶
Overview ¶
Package zeus provides a lightweight dependency injection container for Go. It allows users to register factories and resolve dependencies at runtime.
Basic usage:
c := zeus.New() c.Provide(func() int { return 42 }) c.Run(func(i int) { fmt.Println(i) // Outputs: 42 })
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container holds the registered factories for dependency resolution.
func New ¶
func New() *Container
New initializes and returns a new instance of the Container.
Example:
c := zeus.New()
func (*Container) Merge ¶
Merge combines the factories of another container into the current container. If a factory from the other container conflicts with an existing factory in the current container, and they are not identical, a FactoryAlreadyProvidedError is returned.
Example:
containerA := New() containerB := New() containerA.Provide(func() string { return "Hello" }) containerB.Provide(func() int { return 42 }) err := containerA.Merge(containerB) if err != nil { // Handle merge error }
func (*Container) Provide ¶
Provide registers a factory function for dependency resolution. It ensures that the factory is a function, has a valid return type, and checks for duplicate factories. Returns an error if any of these conditions are not met.
Example:
c := zeus.New() c.Provide(func() int { return 42 })
func (*Container) Run ¶
Run executes the provided function by resolving and injecting its dependencies. It ensures that the function has a valid signature and that all dependencies can be resolved. Returns an error if the function signature is invalid or if dependencies cannot be resolved.
Example:
c := zeus.New() c.Provide(func() int { return 42 }) c.Run(func(i int) { fmt.Println(i) // Outputs: 42 })