uber-go/dig

how to use InvokeOption to get object

nonetheless opened this issue · 1 comments

type A struct{}
var container = dig.New()
a1 := A{}
a2 := A{}
container.Provide(a1, dig.Name("a1"))
container.Provide(a2, dig.Name("a2"))

how can I use container.Invoke to get a1 and a2

Go does not provide a way to annotate individual arguments of a function, and
we have not yet found a reasonable API that would let you specify names of
arguments to a function.

So the only way to request named values right now is with parameter objects.

type params struct {
    dig.In
    
    A1 A `name:"a1"`
    A2 A `name:"a2"`
}

container.Invoke(func(p params) {
    fmt.Println(p.A1)
    fmt.Println(p.A2)
})