Golang Configuration Standards
Priority: P1 (STANDARD)
Principles
- 12-Factor App: Store config in environment variables.
- Typed Config: Load config into a struct, validate it immediately.
- Secrets: Never commit secrets. Use env vars or secret managers.
- No Globals: Return a Config struct and inject it.
Libraries
- Standard Lib:
os.Getenvfor simple apps. - Viper: Industry standard for complex configs (supports env, config files, remote config).
- Koanf: Lighter, cleaner alternative to Viper.
- Caarlos0/env: Good for strict struct tagging.
Pattern
- Define
Configstruct. - Load from defaults.
- Override from file (optional).
- Override from Env (priority).
- Validate.
References
- Config Pattern