Environment Variables
Override config from env.
Why Environment Variables
The Twelve-Factor App methodology recommends storing config in the environment. Env vars let the same binary behave differently across dev, staging, and production without editing files.
Viper can layer env vars on top of file config.
Precedence Order
Viper resolves a key by checking sources in priority order (highest first):
- Explicit
Setcalls - Flags
- Environment variables
- Config file
- Defaults
So an env var overrides the file but loses to an explicit Set.
All lessons in this course
- Reading Config Files
- Environment Variables
- Defaults and Watching
- Binding to Structs