golang-spf13-viper

Configure layered Go application settings with spf13/viper precedence rules.

Updated May 28, 2026
One-click install
npx skills add https://github.com/vanstinator/semantic-search --skill golang-spf13-viper
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-spf13-viper
Source: https://github.com/vanstinator/semantic-search/tree/main/.agents/skills/golang-spf13-viper
Command: npx skills add https://github.com/vanstinator/semantic-search --skill golang-spf13-viper

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It prevents broken or surprising configuration behavior in Go services by making spf13/viper’s precedence rules predictable and correctly wired (flags, env vars, files, defaults, and optional remote KV).

Core Features & Use Cases

  • Layered precedence that behaves correctly: Ensures explicit Set and bound flags win over env vars and config files, and that defaults only apply when nothing else provides a value.
  • Correct env var mapping for nested keys: Makes nested viper keys resolve from environment variables by using SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv together.
  • Production-safe struct decoding and testing patterns: Uses mapstructure tags, supports hot reload with validation, and avoids test pollution by using viper.New() per test.
  • Unmarshal reliability for tricky types: Handles duration and weak/typed decoding issues via decode hooks and DecoderConfig options.

Quick Start

Ask your AI coding agent to wire spf13/viper in your Go app so that nested keys (like database.host) resolve from env vars using prefix + key replacer, flags are bound before command execution, missing config files are handled gracefully, and unmarshalling uses mapstructure tags with proper decode hooks.

Frequently Asked Questions about golang-spf13-viper

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How does viper precedence work when combining flags, environment variables, and config files?

Correct viper precedence ensures explicit Set calls and bound flags win over environment variables and config files, while defaults only apply when nothing else provides a value.

How do I map environment variables to nested viper keys in Go?

Mapping nested viper keys from environment variables requires wiring SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv together so structured keys like database.host resolve correctly from the environment.

Why are my Go config unit tests failing due to viper global state pollution?

Global viper state pollution occurs when tests share the default instance; using viper.New() per test ensures deterministic unit testing by isolating configuration state across test cases.

What is the best way to decode viper configuration into Go structs with mapstructure?

Decoding viper configuration into Go structs uses mapstructure-tagged Unmarshal with decode hooks and DecoderConfig options to handle duration and weak-typed fields safely without runtime panics.

How do I handle missing config files gracefully in a Go CLI application?

Missing config files are handled gracefully by checking for ConfigFileNotFoundError during initialization, allowing the application to proceed using environment variables and defaults instead of crashing.

Can I hot reload viper configuration in a Go service with validation?

Viper supports hot reload with validation in Go services by watching config files for changes and re-running unmarshal logic with mapstructure decode hooks to ensure updated values remain valid.