How spf13/viper works
Viper is a Go configuration registry: applications set or bind values, load them from multiple sources, and retrieve typed, merged configuration. The graph preserves the documented source precedence, file and buffer formats, file writing and watching, aliases, and optional remote providers. Source excerpts show the registry and codec implementations; several capability links rely on the README because the relevant runtime sections were not sampled.
Main entry points: viper.go Key lookup · encoding.go Codec registry · file.go Config file I/O · remote.go Remote source API
Components 18
Configuration sources
- Config file I/O
file.go - Config discovery
finder.go - Environment variables
- Flag bindings
flags.go
Configuration registry
- Viper registry
viper.go - Defaults and overrides
viper.go - Key aliases
viper.go - Key lookup
viper.go - Typed accessors
viper.go
Format codecs
- Codec registry
encoding.go - Dotenv codec
internal/encoding/dotenv/codec.go - JSON codec
internal/encoding/json/codec.go - TOML codec
internal/encoding/toml/codec.go - YAML codec
internal/encoding/yaml/codec.go
Remote configuration
- Remote source API
remote.go - Remote adapter
remote/remote.go
Other components
- Go application
- Remote KV stores
Connections 18
- Go application → Viper registry: sets and reads
README.md - Go application → Config file I/O: reads and writes
README.md - Config discovery → Config file I/O: locates files
- Environment variables → Key lookup: provides values
README.md - Flag bindings → Key lookup: provides values
README.md - Defaults and overrides → Key lookup: supplies fallback
README.md - Key aliases → Key lookup: resolves keys
README.md - Config file I/O → Codec registry: decodes and encodes
- Codec registry → Dotenv codec: selects codec
encoding.go - Codec registry → JSON codec: selects codec
encoding.go - Codec registry → TOML codec: selects codec
encoding.go - Codec registry → YAML codec: selects codec
encoding.go - Go application → Remote source API: configures and reads
README.md - Remote source API → Remote adapter: delegates provider calls
remote/remote.go - Remote adapter → Remote KV stores: reads and watches
remote/remote.go - Remote source API → Key lookup: merges remote values
remote.go - Key lookup → Typed accessors: converts values
viper.go - Go application → Config file I/O: watches changes
README.md