Replies: 2 comments 1 reply
-
Just to add a simple example to my above question: package main
import (
"fmt"
"go.uber.org/fx"
)
type Thingy struct {
Value string
}
type Params struct {
fx.In
Thing *Thingy
}
type Result struct {
fx.Out
Thing *Thingy
}
func main() {
fx.New(
fx.Provide(func() Result {
return Result{}
}),
fx.Invoke(func(p Params) {
fmt.Printf("Thing is nil? %t\n", p.Thing == nil)
}),
).Run()
} The output of this states that the |
Beta Was this translation helpful? Give feedback.
-
The optional tag indicates that it is a OK for a dependency to not be provided at all. For example, if you remove the provide from your code example but don't mark I think (an OG contributor that has been around longer can confirm) that the point of adding The optional tag also allows for modules to function differently based on each consumers needs. Some users of a module may have a |
Beta Was this translation helpful? Give feedback.
-
Hello!
I am having some trouble understanding the difference between option and non-optional parameters. As far as I can tell from the documentation, dependency specified in a params (fx.In) struct that do not have constructors providing those parameters should fail. This does not seem to be the case in my implementation. I have a parameter struct with several pointers to other structs, and when those values are not provided by a constructor, the params struct simply contains the nil values. From reading the docs, it seems this should only be the case when specifying the optional struct tag on the properties of the params struct.
Is my understanding correct? If not, then if objects not provided by constructors are simply the zero value, what is the purpose of having the optional param struct tag?
Beta Was this translation helpful? Give feedback.
All reactions