DI
中文说明
Lightweight dependency injection container implements by Go.
GoDoc
Install
go get github.com/gookit/di
Usage
package main
import (
"github.com/gookit/di"
)
func main() {
box := di.New("my-services")
box.Set("service1", "val1")
box.Set("service2", func() (interface, error) {
return &MyApp{}, nil
})
box.Set("service3", func() (interface, error) {
return &MyObject{}, nil
}, true)
v1 := box.Get("service1")
v2 := box.Get("service2").(*MyApp)
v3 := box.Get("service2").(*MyApp)
v4 := box.Get("service3").(*MyObject)
v5 := box.Get("service3").(*MyObject)
}
API Methods
func (c *Container) Set(name string, val interface{}, isFactory ...bool) *Container
func (c *Container) Get(name string) interface{}
func (c *Container) SafeGet(name string) (val interface{}, err error)
func (c *Container) Inject(ptr interface{}) (err error)
Refer
License
MIT