How To Verify Struct Initialization For Completeness
For initializing structs, there is New()
, right? Pass some parameters, done.
But what about really large structs? Parameter lists aren't feasible beyond a certain size. You'd probably resort to filling the struct directly.
Learn debugging with Matt, at 40% off
(and support AppliedGo this way)!
Use the coupon code APPLIEDGO40 at ByteSizeGo.com.
(Affiliate Link)
Then, however, a new problem arises. Consider this large struct has only, or mostly, mandatory fields. You should set all the required values properly. The Go compiler does not help you here. For every forgotten field, the compiler happily assumes the zero value of the field's type.
How can you verify if you don't forget to fill a mandatory field?
Linters to the rescue.
Let the exhauststruct
linter verify your struct initializations and get notified if one or more fields are not explicitly initialized.
The exhauststruct
linter is available standalone or via the meta-linter golanci-lint
.