Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neonxp/di
Dependency inject container for Go with generics
https://github.com/neonxp/di
Last synced: about 1 month ago
JSON representation
Dependency inject container for Go with generics
- Host: GitHub
- URL: https://github.com/neonxp/di
- Owner: neonxp
- License: gpl-3.0
- Created: 2022-07-16T23:50:51.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-12T00:50:20.000Z (about 2 years ago)
- Last Synced: 2023-07-08T08:15:50.658Z (over 1 year ago)
- Language: Go
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Go Dependency Inject Container
❗️ Main repo: [https://gitrepo.ru/neonxp/di](https://gitrepo.ru/neonxp/di). Github is only mirror.
Simple dependecy inject container with generics.
Use for your own risk!
## Usage
### Register dependency
```go
di.Register("service id", func () (*Service, error) { /* construct service */ })
```### Get dependency
Get dependencies by type:
```go
services, err := di.GetByType[Service]()
```Get dependencies by type and id:
```go
service, err := di.Get[Service]("service id")
```Get dependencies by interface:
```go
services, err := di.GetByInterface[Worker]() // Worker is interface for many workers
```### Go doc
```go
package di // import "go.neonxp.dev/di"func Get[T any](id string) (*T, error)
func GetByInterface[Interface any]() ([]Interface, error)
func GetByType[T any]() ([]*T, error)
func Register[T any](id string, constructor func() (*T, error))
```### Example
```go
di.Register("serviceA", func() (*ServiceA, error) { // <- Register service A
return &ServiceA{}, nil
})
di.Register("serviceB", func() (*ServiceB, error) { // <- Register service B, that depends from service A
serviceA, err := di.Get[ServiceA]() // <- Get dependency from container by type
if err != nil {
return nil, err
}return &ServiceB{
ServiceA: serviceA[0],
}, nil
})// Do work ...
service, err := di.Get[ServiceB]("serviceB") // <- Get instantinated service B
if err != nil {
panic(err)
}service.DoStuff() // Output: Hello, world!
// Services ...
type ServiceA struct{}func (d *ServiceA) DoStuff() {
fmt.Println("Hello, world!")
}type ServiceB struct {
ServiceA *ServiceA
}func (d *ServiceB) DoStuff() {
d.ServiceA.DoStuff()
}```
## License
GPLv3