Digo is a lightweight and extensible Dependency Injection container for Go — featuring constructor-based resolution, interface mapping, lifecycle scopes, and recursive instantiation.
Register factory functions (constructors) that the container uses to instantiate types automatically. Supports various types such as:
- Strings, numbers, booleans
- Arrays and slices
- Nested structs and complex object graphs
Map interfaces to their concrete implementations.
This allows the container to resolve abstractions without hardcoding dependencies, making your architecture more flexible and testable.
Supports lifecycle management for registered types:
- Singleton: A single instance shared across the application.
- Transient: A new instance is created on every resolution.
Scoped resolution ensures optimal performance and memory control for large applications.
Dependencies are resolved recursively, including any sub-dependencies required by constructors.
Supports:
- Optional fields (when nil is acceptable)
- Default values (when handled in constructors)
Install the package using go get:
go get github.com/ChidemJean/digo// Instantiating container
container := digo.New()
// Register a concrete implementation
container.Register(NewEmailNotifier, digo.Singleton)
// Register a service that depends on the interface
container.Register(NewAlertService, digo.Transient)
// Map the interface to the implementation
container.RegisterInterface((*Notifier)(nil), &EmailNotifier{})
// Resolve and use
alert := digo.Resolve[Notifier]()
alert.Send()