Populate structs from environment variables with envdecode
masterUse envdecode to map environment variables to Go struct fields using env struct tags. The package supports nested structs and pointers to nested structs, but it will not allocate new pointers to structs for you.
Requirements:
- Fields must be exported (start with a capital letter).
- Structs with no exported fields or no
envtags will return an error during decoding.
type Config struct {
Hostname string `env:"SERVER_HOSTNAME,default=localhost"`
Port uint16 `env:"SERVER_PORT,default=8080"`
}
var cfg Config
err := envdecode.Decode(&cfg)