
Golang Spf13 Viper
Golang configuration library using spf13/viper, layered precedence (flag > env > file...
Install
npx skills add samber/cc-skills-golang --skill golang-spf13-viper -g -a claude-code -y- Repository
- samber/cc-skills-golang3.8k
- Category
- Testing
- Updated
- 3d ago
- License
- MIT
- Context cost
- Mediumabout 2,222 tokens
- Runs scripts
- No, instructions only
What it does
Golang configuration library using spf13/viper, layered precedence (flag > env > file > KV > default), BindPFlag/BindPFlags, SetEnvPrefix + SetEnvKeyReplacer + AutomaticEnv, ReadInConfig + ConfigFileNotFoundError, Unmarshal + mapstructure struct tags, Sub for sub-trees, WatchConfig + OnConfigChange for hot reload, viper.New() for test isolation, and remote KV integration. Apply when using or adopting spf13/viper, or when the codebase imports `github.com/spf13/viper`. For CLI command structure alongside viper, see the `samber/cc-skills-golang@golang-spf13-cobra` skill. For general CLI architecture, see `samber/cc-skills-golang@golang-cli`.
Persona: You are a Go engineer who treats configuration as a layered system. Flag beats env beats file beats default, and you bind every key so all four layers stay reachable through one API.
Using spf13/viper for layered configuration in Go
Viper resolves configuration values from multiple sources in a fixed precedence order. It has no user-facing surface, it doesn't define commands or flags. Its job is to answer "what is the value of key X right now?" by walking its source layers from highest to lowest priority.
Official Resources:
This skill is not exhaustive, refer to library documentation and code examples for more information:
- For Go package docs, symbols, versions, importers, and known vulnerabilities, → See
samber/cc-skills-golang@golang-pkg-go-devskill (godig), preferred over Context7 for Go package facts. - To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See
samber/cc-skills-golang@golang-goplsskill (gopls). - Context7 remains a fallback for docs not indexed on pkg.go.dev.
go get github.com/spf13/viper@latest
Viper vs. cobra
Cobra owns the command tree, subcommands, flags, arg validation, completions. Viper owns configuration resolution, it answers "what is the value of key X?" by walking its source layers, with no user-facing surface of its own: it is purely a key-value resolver.
- Cobra alone, flag-only CLIs.
- Viper alone, config-file daemons.
- Both, bind flags at
PersistentPreRunEviaBindPFlag.
→ See samber/cc-skills-golang@golang-spf13-cobra for the cobra side of this integration.
The precedence pipeline
Viper resolves a key by walking sources in this order (first set value wins):


