I want to have a static assert or linter for my go code that checks that I do not – by mistake – initialise the structs in my config file with default values for any field. I want everything to be set explicitly.
Is there a way to achieve this?
EDIT: Clarified question.
Check out the validator package. https://gopkg.in/go-playground/validator.v9
We use it extensively in our code to validate structs. e.g.
type Config struct {
Url string `validate:"required"`
MaxHops int `validate:"omitempty,min=0"`
MaxTerms int `validate:"omitempty,min=0"`
MaxCost float64 `validate:"omitempty,min=0"`
}
func Init(cfg *Config) error {
if err := validator.New().Struct(cfg); err != nil {
return errors.Wrap(err, "error in config")
}
//do something
}