Show HN: Defaults – Simple default values in go structs
github.comVery interesting.
I see what might be a problem, however: determining the difference between a zero-value in a struct that was supplied intentionally, or one that was left unspecified.
To clarify what I mean, in the example on that page the following line doesn't produce the right answer:
p := defaults.NewWithDefaults(Person{ConvictedFelon: false}).(Person)
Of course, you could perform the assignment after the defaults have been initialized, but then you're giving up the optional struct initialization that Go provides. I'll have to think about how you could solve this. In the meanwhile, I'll open an issue on Github.My practice has usually been to ensure that the zero values of the types ARE a sane default for a struct if that struct is public and may be instantiated by a client. It sometimes involves flipping the semantics of some Boolean values around.
That, and judicious use of a New() function to construct and initialize types, help make up for the lack of default attributes in structs.