Loads configuration settings into a struct from the following sources in this order.
- default specified in struct tag
- config.yml YAML file
- environment variables
- command line parameters
Each source successively overrides the previous ones.
Nested structs are supported as each setting specifies its full path to the associated struct field.
See the examples folder and the unit tests for how to define and consume your configuration.
The following config struct will contain our settings. Note the default values defined in the struct tags.
var cfg struct {
Address string `yaml:"address" default:"localhost"`
Timeout time.Duration `yaml:"timeout" default:"5m"`
}
config.yml file overrides any struct defaults.
---
address: http://example.com/
timeout: 1m
Environment variables override any matching config.yml file values.
export ADDRESS=http://example.com/funapp
export TIMEOUT=1m30s
Command line arguments override any matching environment variables.
./myapp address=http://example.com/home timeout=2m