https://github.com/actforgood/xdi
Golang Dependency Injection Package
https://github.com/actforgood/xdi
dependency-injection go xdi
Last synced: 4 months ago
JSON representation
Golang Dependency Injection Package
- Host: GitHub
- URL: https://github.com/actforgood/xdi
- Owner: actforgood
- License: mit
- Created: 2022-05-04T12:03:07.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-12-15T17:02:39.000Z (6 months ago)
- Last Synced: 2026-01-12T00:27:04.424Z (5 months ago)
- Topics: dependency-injection, go, xdi
- Language: Go
- Homepage:
- Size: 58.6 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
README
# Xdi
[](https://github.com/actforgood/xdi/actions/workflows/build.yml)
[](https://raw.githubusercontent.com/actforgood/xdi/main/LICENSE)
[](https://coveralls.io/github/actforgood/xdi?branch=main)
[](https://goreportcard.com/report/github.com/actforgood/xdi)
[](https://pkg.go.dev/github.com/actforgood/xdi)
---
Package `xdi` provides a centralized dependency injection manager which holds definitions for an application's objects/dependencies.
### Installation
```shell
$ go get github.com/actforgood/xdi
```
### Example
Basic example:
```go
// DiManager holds application's objects, dependencies.
// Do not inject it/use it directly, in your application's objects.
// It should be used only in the bootstrap process of your application and/or main.go,
// as a centralized container of dependencies.
// Note: instead of declaring a variable, you can also use the singleton provided by xdi.ManagerInstance().
var DiManager = xdi.NewManager()
func init() {
DiManager.AddDefinition(xdi.Definition{
ID: "app.repository.product",
Initializer: func() any {
return NewDummyProductRepository()
},
Shared: true,
})
}
func init() {
DiManager.AddDefinition(xdi.Definition{
ID: "app.service.product",
Initializer: func() any {
return NewDummyProductService(
DiManager.Get("app.repository.product").(ProductRepository),
)
},
Shared: true,
})
}
func main() {
productService := DiManager.Get("app.service.product").(ProductService)
isAvailable, _ := productService.CheckAvailability("some-sku", 2)
fmt.Println("isAvailable:", isAvailable)
}
```
### Misc
Feel free to use this pkg if you like it and fits your needs.
As it is a light/lite pkg, you can also just copy-paste the code, instead of importing it, keeping the license header.
### License
This package is released under a MIT license. See [LICENSE](LICENSE).