What problem does it solve?
It prevents misconfigured Go services by ensuring configuration values resolve correctly across files, environment variables, and flags, following a clear precedence order.
Core Features & Use Cases
- Layered precedence you can reason about: resolves values in the fixed order of Set() > bound flags > env vars > config file(s) > remote KV > defaults, reducing “why is my value ignored?” bugs.
- Correct env + nested key handling: supports SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv so keys like database.host map to MYAPP_DATABASE_HOST.
- Safe struct decoding and hot reload: unmarshals into structs via mapstructure tags, optionally supports decode hooks, and enables WatchConfig/OnConfigChange patterns for hot reload with reload-time validation.
- Test isolation with viper.New(): avoids global-state pollution by using per-test Viper instances.
Quick Start
Configure your Go service to use layered viper configuration by enabling AutomaticEnv with SetEnvPrefix and SetEnvKeyReplacer, then unmarshal into a typed config struct using mapstructure tags and ensure flag bindings happen before Execute().