Provides a dependency injection mechanism for Golang.
With Go module support, simply add the following import
import "github.com/rapatao/go-injector"
to your code, and then go [build|run|test]
will automatically fetch the necessary dependencies.
Otherwise, run the following Go command to install the injector package:
go get -u github.com/rapatao/go-injector
First you need to import Injector package for using it, one simplest example likes the follow example.go
:
package main
import (
"fmt"
"github.com/rapatao/go-injector"
)
type A struct {
name string
b B
}
type B struct {
name string
}
func (a *A) Run() {
fmt.Printf("%s, %s", a.name, a.b.name)
}
func (a *A) Initialize(container *injector.Container) error {
a.name = "hello"
var b B
err := container.Get(&b)
a.b = b
return err
}
func (b *B) Initialize(_ *injector.Container) error {
b.name = "world"
return nil
}
func main() {
container := injector.Create()
var a A
err := container.Get(&a)
if err != nil {
panic(err)
}
a.Run()
}
And use the Go command to run the demo:
go run example.go
Please see CONTRIBUTING for details on submitting patches and the contribution workflow.