Viper
Configuration solution for Go that merges files, environment variables, flags, and remote stores with precedence.
Official Documentation ↗v1.19. Check the official docs for the latest API surface.Installation
go get github.com/spf13/viperMethods
No methods match this filter.
Configuration solution for Go that merges files, environment variables, flags, and remote stores with precedence.
Overview
Viper is the configuration hub for Go applications: it reads YAML, JSON, TOML, and other files, overlays environment variables and flags, applies defaults, and can watch files for changes. Keys are dot paths over the merged tree, and getters cast values on read.
Unmarshal projects the merged configuration into typed structs, while SetEnvPrefix and key replacers align env var naming with nested keys. It is the standard partner to Cobra for complete CLI configuration.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
SetConfigFile / ReadInConfig— Loads an explicit config file; the format is inferred from the extension.GetString / GetInt / GetBool— Reads values by dot-delimited path with casting across merged sources.SetDefault / AutomaticEnv— Registers fallback values and maps config keys onto uppercase environment variables.WatchConfig / Unmarshal— Reacts to config file edits at runtime and decodes the merged tree into a typed struct.
Usage Notes
- Unmarshal into one typed config struct at startup instead of scattering viper.Get across the codebase.
- Set defaults for every key so a missing file still boots with sane behavior.
- Use IsSet to fail fast on required keys rather than discovering zero values later.
Common Pitfalls
- AutomaticEnv plus nested keys needs a replacer; otherwise env overrides never fire.
- mapstructure tags, not json tags, drive Unmarshal, and mismatches silently zero fields.
- WatchConfig callbacks see the new values but must re-Unmarshal to refresh your struct.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/spf13/viper@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:53:22.633Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- The zero value will also be returned if the value is set, but fails to parse as the requested type. (https://cdn.jsdelivr.net/gh/spf13/viper@latest/README.md)
FAQ
- What is the precedence order?
- Explicit Set beats flags, which beat environment variables, which beat config files, which beat key-value stores, which beat defaults. Knowing this ladder explains most surprising values when several sources define the same key.
- Why does AutomaticEnv miss my keys?
- AutomaticEnv only answers Get calls for keys you ask about, and by default maps dots directly, which rarely matches environment naming. Combine SetEnvKeyReplacer to turn server.port into SERVER_PORT and use BindEnv for keys never present in the file.
- How do I validate the final configuration?
- Unmarshal into a struct tagged with mapstructure, then validate that struct with validator tags or explicit checks at startup. Failing fast with a precise message beats discovering a zero-value port at first request.
Report a problem with this API docOpens a pre-filled issue on GitHub.