https://github.com/livebud/di
Dependency injection using Generics.
https://github.com/livebud/di
Last synced: 10 months ago
JSON representation
Dependency injection using Generics.
- Host: GitHub
- URL: https://github.com/livebud/di
- Owner: livebud
- License: mit
- Created: 2023-08-01T04:52:05.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-08T23:31:28.000Z (over 2 years ago)
- Last Synced: 2024-05-01T09:42:02.780Z (about 2 years ago)
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
- License: License.md
Awesome Lists containing this project
README
# DI
[](https://pkg.go.dev/github.com/livebud/di)
Dependency injection using Generics.
## Features
- Provide dependencies in a natural and type-safe way
- Register dependencies with other dependencies (e.g. register a controller with the router)
- Swap out dependencies during testing
- Middleware support
- Unmarshal dependencies into a struct
## Install
```
go get github.com/livebud/di
```
## Example
In the following example, we load the logger which loads the environment:
```go
type Env struct {
DatabaseURL string
}
func provideEnv(in di.Injector) (*Env, error) {
return &Env{
DatabaseURL: "postgres://localhost:5432/db",
}, nil
}
type Log struct {
env *Env
}
func provideLog(in di.Injector) (*Log, error) {
env, err := di.Load[*Env](in)
if err != nil {
return nil, err
}
return &Log{env}, nil
}
func main() {
in := di.New()
di.Provide(in, provideEnv)
di.Provide(in, provideLog)
log, err := di.Load[*Log](in)
fmt.Println(log.env.DatabaseURL)
}
```
## Contributors
- Matt Mueller ([@mattmueller](https://twitter.com/mattmueller))
## License
MIT