ioc overview
Resolve dependencies by type, construct singletons on demand, and close resources in reverse construction order.
Use ioc to assemble configuration, connections, and application services at startup. A Container identifies services by type. Register factories with Container.Provide. The first call to Container.Resolve builds the service, and successful results are reused within the same container. Register existing values with Container.Supply.
Installation
You need Go 1.27 or later. Run the installation command in your Go module.
go get github.com/luminyra/iocTypes, names, and lifecycle
- Resolve by type when that type has one default service. Use
Keyto name multiple services of the same type and retain their return type. Container.Checkchecks constructed services that implementChecker.Container.Closecloses services in reverse construction order so consumers close before their dependencies.
Go standard library integration
ioc uses Go 1.27 generic methods for type-based resolution. Factories state which dependencies they resolve. Resource cleanup supports standard-library io.Closer and types such as *http.Server with a Shutdown(ctx) method.
Performance
Resolving an already-built service takes about 14 ns. Resolving it through a Key takes about 11 ns. Neither allocates memory.
The quickstart starts with the dependency chain for a configuration value, database, and repository.