0Pricing
Go Academy · Lesson

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 Set calls
  • Flags
  • Environment variables
  • Config file
  • Defaults

So an env var overrides the file but loses to an explicit Set.

All lessons in this course

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