Simple Golang DI container based on reflection
Examples: examples folder
Full example: noartem/godi-example
Godoc: pkg.go.dev
-
Install godi
go get github.com/noartem/godi
-
Create interfaces and beans implementing these interfaces:
type IName interface { NewName() string } type Name struct {} func (name *Name) Generate() string { return "Lorem Ipsumovich" }
-
Create bean factory:
func NewName() IName { return &Name{ ... } }
In factory, you can import other beans:
func NewName(db IDatabase, log ILogger, ...) IName { return &Name{ db: db, log: log, } }
Factories can also return BeanOptions and/or errors:
func NewName() (IName, *godi.BeanOptions, error) { err := someFunc() if err != nil { return &Name{}, nil, err } options := &godi.BeanOptions{ Type: godi.Singleton, // Default: godi.Prototype } name := &Name{} return name, options, nil }
or
func NewName() (IName, error) {}
, orfunc NewName() (IName, *godi.BeanOptions)
-
Create DI container and register factories:
func main() { c, err := godi.NewContainer(NewName, NewRandom, NewFoo, ...) if err != nil { panic(err) } ...
-
Get bean from a container:
// get bean by interface name nameBean, err := c.Get("IName") if err != nil { panic(err) } name, ok := nameBean.(IName) if !ok { panic("Invalid name bean") } // now you can use IName fmt.Println(name.Generate()) }
-
Profit! See another examples in examples folder
-
Static beans. Can be used for sharing constants (global config, secrets, e.t.c.)
-
Create a custom type
type IPassword string
-
Create implementations
var defaultPassword IPassword = "qwerty123"
-
Register
godi.NewContainer(defaultPassword)
Constant will be registered as
IPassword
-
Use it
func NewName(password IPassword) IName {...}
-
-
Structures with dependencies in Input. If you don't want to write boilerplate code creating structure with all input dependency, you can write them in structure with
InStruct
field and require this as input in factory.-
Create structure based on godi.InStruct and add your dependencies:
import "github.com/noartem/godi" type deps struct { godi.InStruct Name IName Config IConfig Random IRandom }
-
Require this structure in factory:
func NewHello(deps deps) IHello { ... }
-
Use your dependencies:
func NewHello(deps deps) IHello { log.Println(deps.Name.GenerateName()) log.Println(deps.Config) log.Println(deps.Random.Intn(1337)) }
-