Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/defval/di
🛠A full-featured dependency injection container for go programming language.
https://github.com/defval/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/defval/di
- Owner: defval
- License: mit
- Created: 2020-02-03T19:06:39.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-16T19:46:52.000Z (12 months ago)
- Last Synced: 2024-04-22T00:21:19.257Z (8 months ago)
- Topics: dependency-injection, di, go, ioc, ioc-container
- Language: Go
- Homepage:
- Size: 305 KB
- Stars: 226
- Watchers: 10
- Forks: 13
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - di - A dependency injection container for go programming language. (Miscellaneous / Dependency Injection)
- zero-alloc-awesome-go - di - A dependency injection container for go programming language. (Miscellaneous / Dependency Injection)
- awesome-go-extra - di - featured dependency injection container for go programming language.|156|9|1|2020-02-03T19:06:39Z|2021-11-30T00:02:18Z| (Microsoft Office / Dependency Injection)
README
# DI
[![Documentation](https://img.shields.io/badge/godoc-reference-blue.svg?style=for-the-badge&logo=go)](https://pkg.go.dev/github.com/defval/di)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/defval/di?logo=semver&style=for-the-badge)](https://github.com/defval/di/releases/latest)
[![GitHub Workflow Status (with branch)](https://img.shields.io/github/actions/workflow/status/defval/di/go.yml?branch=master&logo=github-actions&style=for-the-badge)](https://github.com/defval/di/actions/workflows/go.yml)
[![Go Report Card](https://img.shields.io/badge/go%20report-A%2B-green?style=for-the-badge)](https://goreportcard.com/report/github.com/defval/di)
[![Codecov](https://img.shields.io/codecov/c/github/defval/di?logo=codecov&style=for-the-badge)](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 mainimport (
"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.