PluginBench
Skill
Pass
Audit score 90

golang-spf13-viper

samber/cc-skills-golang

Layered configuration resolution for Go: flags, env vars, files, and defaults in fixed precedence order.

What is golang-spf13-viper?

spf13/viper is a Go configuration library that resolves values from multiple sources (flags, environment variables, config files, remote KV, defaults) in a fixed precedence pipeline. Use it when you need to support configuration from multiple sources with clear priority rules, especially alongside cobra for CLI applications.

  • Resolve configuration keys across four layers: flags > env vars > config files > defaults, with explicit Set() at highest priority
  • Bind cobra flags and environment variables to viper keys with BindPFlag/BindPFlags and AutomaticEnv
  • Load and merge configuration from JSON, TOML, YAML, HCL, INI, and properties files with ReadInConfig and MergeInConfig
  • Unmarshal resolved configuration into Go structs using mapstructure tags for type-safe access
  • Watch config files for changes and trigger callbacks via WatchConfig and OnConfigChange for hot reload
  • Create isolated viper instances with viper.New() for test isolation and avoid global state leaks

How to install golang-spf13-viper

npx skills add https://github.com/samber/cc-skills-golang --skill golang-spf13-viper
Prerequisites
  • Go 1.11 or later
  • github.com/spf13/viper installed via go get github.com/spf13/viper@latest
  • (optional) github.com/spf13/cobra if binding flags to viper
Claude Code
Cursor
Windsurf
Cline

How to use golang-spf13-viper

  1. 1.Import viper and set the config file name and search paths using SetConfigName and AddConfigPath
  2. 2.Call SetEnvPrefix, SetEnvKeyReplacer with a strings.Replacer (e.g., dot to underscore), and AutomaticEnv to enable environment variable resolution
  3. 3.Bind cobra flags to viper keys using BindPFlag or BindPFlags in init() or PersistentPreRunE, never in RunE
  4. 4.Call ReadInConfig and handle ConfigFileNotFoundError gracefully (config files are optional)
  5. 5.Define a config struct with mapstructure tags on all fields and call viper.Unmarshal(&cfg) to populate it
  6. 6.(optional) Call WatchConfig and OnConfigChange to enable hot reload of configuration changes

Use cases

Good for
  • CLI applications that accept configuration from flags, environment variables, and config files simultaneously
  • Daemon services that load configuration from files and environment overrides without command-line flags
  • Multi-environment deployments where dev/staging/prod configs are files but secrets come from env vars
  • Applications requiring hot-reload of configuration without restart when files change
  • Testing scenarios where each test needs its own isolated configuration state
Who it's for
  • Go backend engineers building CLI tools or services with cobra
  • DevOps engineers managing multi-source configuration for containerized applications
  • Teams adopting 12-factor app principles with environment-based configuration
  • Developers maintaining existing codebases that import github.com/spf13/viper

golang-spf13-viper FAQ

Why doesn't my environment variable override my config file value?

You must call SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv together at startup. Missing any one breaks nested key resolution — viper will look for the wrong env var name (e.g., MYAPP_DATABASE.HOST instead of MYAPP_DATABASE_HOST).

Should I use the global viper instance or viper.New()?

Never use the global viper in tests — state leaks across test cases. Use viper.New() per test for isolation. The global instance is acceptable in application code.

Why does my config file not load?

ReadInConfig returns ConfigFileNotFoundError when the file is missing. You must handle this error gracefully with errors.As() — missing config files should not crash services that run with only flags and env vars.

When should I bind flags to viper?

Bind flags in init() or PersistentPreRunE, never in RunE. Config loading in PersistentPreRunE runs before RunE, so bindings set in RunE are missed.

What is the precedence order for configuration sources?

Viper walks sources in fixed order: explicit Set() > flags > env vars > config files > remote KV > defaults. The first source that has a value wins; you cannot reorder this pipeline.

Full instructions (SKILL.md)

Source of truth, from samber/cc-skills-golang.


name: golang-spf13-viper description: "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." user-invocable: true license: MIT compatibility: Designed for Claude Code, Codex or similar harness, and for projects using Golang. metadata: author: samber version: "1.1.2" openclaw: emoji: "🔧" homepage: https://github.com/samber/cc-skills-golang requires: bins: - go install: [] skill-library-version: "1.21.0" allowed-tools: Read Edit Write Glob Grep Bash(go:) Bash(golangci-lint:) Bash(git:) Agent WebFetch mcp__context7__resolve-library-id mcp__context7__query-docs Bash(godig:) Bash(gopls:) LSP mcp__gopls__ paths:

  • "**/*.go"

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-dev skill (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-gopls skill (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 PersistentPreRunE via BindPFlag.

→ 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):

1. explicit Set()      — viper.Set("key", val)    highest priority
2. flag                — bound pflag.Flag
3. env var             — BindEnv / AutomaticEnv
4. config file         — ReadInConfig / MergeInConfig
5. KV remote           — etcd / Consul
6. default             — viper.SetDefault("key", val)   lowest priority

This pipeline is fixed and cannot be reordered. Understanding it prevents most viper bugs: a key that "should" come from a config file may be shadowed by an env var or a flag with a default value.

Sources and config files

viper.SetConfigName("config")
viper.AddConfigPath("$HOME/.myapp")
if err := viper.ReadInConfig(); err != nil {
    var notFound *viper.ConfigFileNotFoundError
    if !errors.As(err, &notFound) {
        return fmt.Errorf("reading config: %w", err) // propagate real errors only
    }
}

ConfigFileNotFoundError must be handled gracefully — config files are usually optional. An unhandled error from a missing file crashes programs that are perfectly valid when run with only flags or env vars.

For supported formats (JSON, TOML, YAML, HCL, INI, properties), MergeInConfig, and remote KV, see sources-and-formats.md.

Env binding and key replacers

This is the highest-bug-density area in viper. All three settings must be wired together — missing any one breaks nested key resolution:

// ✓ Good — all three wired together at startup
viper.SetEnvPrefix("MYAPP")                             // prevent collisions: PORT → MYAPP_PORT
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))  // database.host → MYAPP_DATABASE_HOST
viper.AutomaticEnv()

// ✗ Bad — without SetEnvKeyReplacer, viper looks for MYAPP_DATABASE.HOST (dot preserved)

For BindEnv, AllowEmptyEnv, and env-vs-default interaction, see binding-and-env.md.

Flag binding (the cobra seam)

Bind cobra flags to viper in init() or PersistentPreRunE — never in RunE (config loading in PersistentPreRunE already ran before RunE, so bindings set in RunE are missed):

func init() {
    rootCmd.PersistentFlags().Int("port", 8080, "listen port")
    viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
    // viper.BindPFlags(cmd.Flags()) — bind an entire FlagSet at once
}

For AllowEmptyEnv and flag/env interaction details, see binding-and-env.md.

Unmarshaling into structs

viper.Unmarshal maps the resolved configuration into a struct using mapstructure:

type Config struct {
    Port     int `mapstructure:"port"`
    Database struct {
        MaxConn int `mapstructure:"max_conn"` // explicit tag: mapstructure won't convert underscore→camelCase
    } `mapstructure:"database"`
}
var cfg Config
viper.Unmarshal(&cfg)

Always use mapstructure tags — implicit mapping is fragile for nested structs and underscore-named fields. Prefer UnmarshalKey("database", &dbCfg) over Sub("database").Unmarshal — it avoids the nil-check Sub requires when the key is missing.

For time.Duration / net.IP / slice decoders and custom DecodeHook registration, see unmarshal.md.

Sub-trees

viper.Sub("database") returns a new *viper.Viper scoped to the prefix, or nil if the key does not exist — always nil-check before calling methods on the result. Prefer UnmarshalKey("database", &dbCfg) which avoids the nil risk entirely.

Hot reload

viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) { /* re-apply changed values */ })

WatchConfig uses fsnotify and watches inodes, so editors that write atomically via rename (vim, neovim) replace the inode and the callback may not fire. Test hot-reload with echo >> config.yaml, not editor saves. For race-safe reload patterns, see watch-and-reload.md.

Test isolation

Never use the global viper in tests — state leaks across test cases. Use viper.New() per test so each instance is isolated:

v := viper.New()
v.SetConfigFile("testdata/config.yaml")
require.NoError(t, v.ReadInConfig())

For t.Setenv interactions and Reset() limitations, see testing-and-isolation.md.

Best Practices

  1. Set prefix + key replacer + AutomaticEnv together — missing any one causes nested env keys to silently not resolve (database.host → DATABASE.HOST instead of DATABASE_HOST).
  2. Handle ConfigFileNotFoundError gracefully — a missing config file should not crash a service that runs with only flags and env vars.
  3. Always use mapstructure tags on config structs — implicit mapping silently misses nested and underscore-named fields.
  4. Use viper.New() in tests, never the global — the global accumulates state across test runs; per-test instances are isolated.
  5. Bind flags before Execute() — binding in RunE is too late; cobra parses flags before RunE runs.

Common Mistakes

MistakeWhy it failsFix
AutomaticEnv without SetEnvKeyReplacerdatabase.host looks for MYAPP_DATABASE.HOST (dot preserved) — never matchesAdd SetEnvKeyReplacer(strings.NewReplacer(".", "_")) before AutomaticEnv
No mapstructure tags on struct fieldsSilently misses nested and underscore-named fieldsAdd mapstructure:"key_name" to every field
Using global viper in testsState from one test contaminates the next, causing flaky orderingCreate viper.New() per test
Missing ConfigFileNotFoundError checkMissing config file crashes a service that should run on flags/env aloneerrors.As(err, &notFound) — only propagate non-not-found errors

Further Reading

  • sources-and-formats.md — supported file formats, multi-path search, MergeInConfig, remote KV (etcd/Consul)
  • binding-and-env.md — BindEnv, AutomaticEnv, SetEnvPrefix, SetEnvKeyReplacer, AllowEmptyEnv, timing rules
  • unmarshal.md — Unmarshal, UnmarshalKey, mapstructure tags, custom DecodeHooks (Duration, IP, slice)
  • watch-and-reload.md — WatchConfig, OnConfigChange, fsnotify caveats, atomic-rename trap, race-safe patterns
  • testing-and-isolation.md — viper.New() per test, t.Setenv interactions, Reset() limitations, snapshot/restore

Cross-References

  • → See samber/cc-skills-golang@golang-cli skill for general CLI architecture — project layout, exit codes, signal handling, cobra+viper integration
  • → See samber/cc-skills-golang@golang-spf13-cobra skill for the cobra side of this integration (flag definition and binding)
  • → See samber/cc-skills-golang@golang-testing skill for general Go testing patterns

If you encounter a bug or unexpected behavior in spf13/viper, open an issue at https://github.com/spf13/viper/issues.