0PricingLogin
Go Academy · Lesson

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

  1. Reading Config Files
  2. Environment Variables
  3. Defaults and Watching
  4. Binding to Structs
← Back to Go Academy