go-simpler/env

default values from the `default` tag

tmzane opened this issue · 0 comments

Consider adding another option to provide default values - using the default struct tag. It might be useful for large configs with many environment variables: specifying the default value on the same line with the variable's name greatly improves readability. On the other hand, it completely ruins type safety (everything is a string), so the compiler won't be able to detect type-related errors. Also, having two different ways (struct initialization and the default tag) to do the same thing is probably a poor API design 🤔

Anyway, here is an example:

var cfg struct {
	Port int `env:"PORT" default:"8080"`
}
if err := env.Load(&cfg); err != nil {
	// handle error
}