https://github.com/d3fvxl/di
🛠A full-featured dependency injection container for go programming language.
https://github.com/d3fvxl/di
dependency-injection di go ioc ioc-container
Last synced: about 2 months ago
JSON representation
🛠A full-featured dependency injection container for go programming language.
- Host: GitHub
- URL: https://github.com/d3fvxl/di
- Owner: d3fvxl
- License: mit
- Created: 2020-02-03T19:06:39.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-16T19:46:52.000Z (about 2 years ago)
- Last Synced: 2025-10-09T12:03:05.639Z (5 months ago)
- Topics: dependency-injection, di, go, ioc, ioc-container
- Language: Go
- Homepage:
- Size: 305 KB
- Stars: 238
- Watchers: 9
- Forks: 13
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- zero-alloc-awesome-go - di - A dependency injection container for go programming language. (Miscellaneous / Dependency Injection)
- awesome-go - d3fvxl/di - featured dependency injection container for go programming language. ☆`239` (Miscellaneous / Dependency Injection)
README
# DI
[](https://pkg.go.dev/github.com/defval/di)
[](https://github.com/defval/di/releases/latest)
[](https://github.com/defval/di/actions/workflows/go.yml)
[](https://goreportcard.com/report/github.com/defval/di)
[](https://codecov.io/gh/defval/di)
**DI** is a dependency injection library for the Go programming language.
Dependency injection is a form of inversion of control that increases modularity and extensibility in your programs.
This library helps you organize responsibilities in your codebase and makes it easy to combine low-level implementations
into high-level behavior without boilerplate.
## Features
- Intuitive auto wiring
- Interface implementations
- Constructor injection
- Optional injection
- Field injection
- Lazy-loading
- Tagging
- Grouping
- Iteration
- Decoration
- Cleanup
- Container Chaining / Scopes
## Installation
```shell
go get github.com/defval/di
```
## Documentation
You can use the standard [pkg.go.dev](https://pkg.go.dev/github.com/defval/di) and inline code comments. If you are new
to auto-wiring libraries such as [google/wire](https://github.com/google/wire)
or [uber-go/dig](https://github.com/uber-go/dig), start with the [tutorial](./docs/tutorial.md).
### Essential Reading
- [Tutorial](./docs/tutorial.md)
- [Examples](./_examples)
- [Advanced Features](./docs/advanced.md)
## Example Usage
```go
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/defval/di"
)
func main() {
di.SetTracer(&di.StdTracer{})
// create container
c, err := di.New(
di.Provide(NewContext), // provide application context
di.Provide(NewServer), // provide http server
di.Provide(NewServeMux), // provide http serve mux
// controllers as []Controller group
di.Provide(NewOrderController, di.As(new(Controller))),
di.Provide(NewUserController, di.As(new(Controller))),
)
// handle container errors
if err != nil {
log.Fatal(err)
}
// invoke function
if err := c.Invoke(StartServer); err != nil {
log.Fatal(err)
}
}
```
Full code available [here](./_examples/tutorial/main.go).
## Questions
If you have any questions, feel free to create an issue.