Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pkg6/gdi
A simple yet powerful dependency injection container for Go.
https://github.com/pkg6/gdi
Last synced: 7 days ago
JSON representation
A simple yet powerful dependency injection container for Go.
- Host: GitHub
- URL: https://github.com/pkg6/gdi
- Owner: pkg6
- License: apache-2.0
- Created: 2023-06-14T13:16:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-15T09:39:52.000Z (11 months ago)
- Last Synced: 2024-04-16T00:13:03.568Z (7 months ago)
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A simple yet powerful dependency injection container for Go.
## Installation
```
go get -u github.com/pkg6/gdi
```## Usage
Create a container
```
container := container.New()
```## Set a value
```
container.Set("foo", "bar")
```## Get a value
```
value,err:= container.Get("foo") // value is "bar"
```## Check if a value exists
```
exists := container.Exists("foo") // exists is true
```## Unset a value
```
container.Unset("foo")
```## Register a service provider
```
type DemoServiceProvider struct {
}
func (d DemoServiceProvider) Register(container IContainer) {
container.Set("foo", "bar")
}
sp := &DemoServiceProvider{}
container.Register(sp)
```## Register a Object
```
type Foo struct {
container IContainer
}func (f *Foo) Construct(container IContainer) {
f.container = container
}foo := &Foo{}
c.Set("foo", foo)
```The service provider can then set values in the container
## Register a HandlerFunc
```
c.Handler(func(container IContainer) {
container.Set("handler", "handler")
})
```The service provider can then set values in the container
## Panic handling
Methods like Get and Raw will panic if the identifier does not exist. You can recover from panics to handle the error in a graceful way:
```
defer func() {
if err := recover(); err != nil {
// handle error
}
}()
value = container.Get("not-exist") // Will panic
```## Contributors
[@pkg6](https://github.com/pkg6/gdi)
## License
Container is licensed under the MIT license. See the [LICENSE](https://github.com/pkg6/gdi/blob/main/LICENSE) file for details.