Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ncrypthic/gocontainer
Simple dependency injection service container in golang
https://github.com/ncrypthic/gocontainer
dependency-injection golang
Last synced: 13 days ago
JSON representation
Simple dependency injection service container in golang
- Host: GitHub
- URL: https://github.com/ncrypthic/gocontainer
- Owner: ncrypthic
- License: mit
- Created: 2017-01-13T08:40:09.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-07T16:43:04.000Z (over 6 years ago)
- Last Synced: 2024-10-11T18:32:46.363Z (about 1 month ago)
- Topics: dependency-injection, golang
- Language: Go
- Size: 10.7 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go-container
[![GoDoc](https://godoc.org/github.com/ncrypthic/gocontainer?status.svg)](https://godoc.org/github.com/ncrypthic/gocontainer)
[![Go Report Card](https://goreportcard.com/badge/github.com/ncrypthic/gocontainer)](https://goreportcard.com/report/github.com/ncrypthic/gocontainer)Simple dependency injection service container for golang
## Usage
```go
// app/main.go
package mainimport (
"fmt"
"log""github.com/ncrypthic/gocontainer"
)type Config struct {
UserServiceUrl string
}type Service struct {
// Will be injected by service container
Config config.Config `inject:"config"`
}func main() {
config := Config{
UserServiceUrl: "http://example.com/users",
}
// no need to manually pass config to user.Service struct
userService := new(user.Service)
container := gocontainer.NewContainer()
container.RegisterService("config", config)
container.RegisterService("userService", userService)
/* To allow service container handling the application process
exit and graceful shutdown, just uncomment the following line */
// container.EnableGracefulShutdown(25 * time.Second )// Populate and inject dependencies
if err := container.Ready(); err != nil {
log.Fatalf("Failed to populate service container! %v", err)
}
// http.ListenAndServe(":8080", nil)
}
```Service container allow clean intialization file by injecting dependencies to every
services in the container.## License
MIT License