Defaults and Watching
Set defaults and hot reload.
Sensible Defaults
Good config systems work even when nothing is set. Defaults give every key a fallback so your program never crashes on a missing value.
Viper provides SetDefault for this, and it sits at the bottom of the precedence chain.
Setting Defaults
Register defaults before reading the file. Any source (file, env, flag) overrides these.
viper.SetDefault("port", 8080)
viper.SetDefault("logLevel", "info")
fmt.Println(viper.GetInt("port"))All lessons in this course
- Reading Config Files
- Environment Variables
- Defaults and Watching
- Binding to Structs