Categorygithub.com/enorith/container
repositorypackage
0.1.0
Repository: https://github.com/enorith/container.git
Documentation: pkg.go.dev

# README

golang implement of IoC-Container

basic usage

package main
import (
    "fmt"
    "github.com/enorith/container"
    "reflect"
)

type Foo struct{
    name string
}

func main(){
    c := container.New()
 
    // bind
    c.BindFunc(Foo{}, func(c *container.Container) (reflect.Value, error) {
        return reflect.ValueOf(Foo{"foo"}), nil
    }, false)
    // get instance
    v, _ := c.Instance(Foo{})
    fmt.Println(v.Interface().(Foo).name)
    var f Foo
    // get instance
    c.InstanceFor(Foo{}, &f)
    fmt.Println(f.name)
    // bind with name
    c.BindFunc("foo", func(c *container.Container) (reflect.Value, error) {
        return reflect.ValueOf(Foo{"foo"}), nil
    }, false)
    v2, _ := c.Instance("foo")
    fmt.Println(v2.Interface().(Foo).name)
}