golang-spf13-viper

Configure layered Viper settings across flags, env vars, files, and defaults.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Viper-backed Go projects often fail to resolve the intended settings because config can come from multiple sources (flags, env vars, files, defaults, and optional remote KV) with a strict precedence order. This skill helps you avoid subtle misconfiguration by wiring the environment and flag bindings correctly and by unmarshaling into typed structs safely.

Core Features & Use Cases

  • Layered precedence correctness: Ensures you understand how Viper decides which value wins across Set(), flags, env vars, config files, remote KV, and defaults.
  • Reliable env var mapping for nested keys: Shows how to configure SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv so keys like database.host map to APP_DATABASE_HOST.
  • Safe struct decoding: Covers Unmarshal and mapstructure struct tags (including why time.Duration and other types may need decode hooks).
  • Operational ergonomics: Handles optional config files via ConfigFileNotFoundError, supports sub-tree access with nil-aware guidance, and explains hot reload caveats with WatchConfig/OnConfigChange.

Quick Start

Tell the agent to configure Viper in your Go service so nested keys are correctly read from environment variables by applying prefix + key replacer + AutomaticEnv, while unmarshaling into your config structs using mapstructure tags.

Frequently Asked Questions about golang-spf13-viper

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

FAQPage Schema
How does Viper resolve configuration precedence across flags, env vars, and files?

Viper resolves Go configuration by following a strict precedence order across flags, environment variables, config files, and defaults. Applying this order correctly ensures the intended setting is selected from multiple sources without subtle misconfigurations.

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

To map nested Go config keys to environment variables, apply SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv together. This ensures keys like database.host correctly resolve to environment variables like APP_DATABASE_HOST.

What is the best way to unmarshal Viper config into typed Go structs?

Unmarshal Viper config into typed Go structs using mapstructure tags. This approach safely decodes configurations, including handling specific types like time.Duration which may require decode hooks for proper mapping.

Does Viper support hot reload for Go configuration files?

Viper supports hot reload in Go by using WatchConfig and OnConfigChange. This enables safe runtime updates when configuration files change, though specific caveats apply during the hot reload process.

How do I handle optional config files with Viper in Go?

Handle optional config files in Viper by gracefully catching ConfigFileNotFoundError. This allows your Go application to start normally when a configuration file is absent rather than failing the startup process.

Can I integrate Viper configuration with Cobra flag parsing?

Yes, you can integrate Viper with Cobra flag parsing in Go. Binding cobra flags to Viper ensures that command-line inputs correctly participate in the layered configuration resolution alongside environment variables and files.