A lightweight, Spring-inspired dependency injection container for Go with support for singleton, prototype, and request-scoped beans.
goioc/di is a dependency injection library for Go that provides an Inversion of Control container to manage application components called beans. It automates dependency injection, supports multiple bean scopes (singleton, prototype, request), and includes features like lifecycle hooks and HTTP middleware. It solves the problem of managing complex dependencies in large Go applications, reducing boilerplate and improving testability.
Go developers building large or enterprise-level applications who want to manage dependencies more efficiently, especially those familiar with dependency injection patterns from other languages like Java.
Developers choose goioc/di for its Spring-like features in a lightweight Go package, including flexible bean registration, automatic injection with struct tags, request-scoped beans for web apps, and built-in middleware. It offers a balance of power and simplicity without the overhead of heavier frameworks.
Simple and yet powerful Dependency Injection for Go
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Supports registration by type, pre-created instance, or factory method with context-aware creation, as shown in the README with examples like `di.RegisterBeanFactory` for dynamic bean instantiation.
Uses struct tags (`di.inject`) for effortless dependency injection, including support for optional dependencies, ID-less auto-wiring, and injection into slices or maps, demonstrated in the weather controller example.
Offers lifecycle hooks via `InitializingBean` and bean post-processors, plus built-in HTTP middleware for request-scoped beans, enabling seamless integration with web frameworks like gorilla/mux.
Detects and reports circular dependencies for prototype and request beans to prevent infinite loops, though singleton circular references are allowed, adding safety for complex dependency graphs.
Requires explicit bean registration using reflection (e.g., `reflect.TypeOf`), as the README admits Go lacks classpath scanning, leading to boilerplate code compared to auto-wiring in frameworks like Spring.
Relies on Go's reflection package for injection and registration, which can introduce runtime overhead and reduce compile-time type safety, making it less ideal for performance-critical applications.
Request-scoped beans cannot be injected into other beans, only accessed via HTTP request context, restricting their use in non-web layers and complicating architecture for some scenarios.
di is an open-source alternative to the following products: