golang-spf13-viper

Resolve layered Go configuration precedence with spf13/viper across flags, env vars, files, and defaults.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It prevents misconfigured Go services by ensuring configuration values resolve correctly across files, environment variables, and flags, following a clear precedence order.

Core Features & Use Cases

  • Layered precedence you can reason about: resolves values in the fixed order of Set() > bound flags > env vars > config file(s) > remote KV > defaults, reducing “why is my value ignored?” bugs.
  • Correct env + nested key handling: supports SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv so keys like database.host map to MYAPP_DATABASE_HOST.
  • Safe struct decoding and hot reload: unmarshals into structs via mapstructure tags, optionally supports decode hooks, and enables WatchConfig/OnConfigChange patterns for hot reload with reload-time validation.
  • Test isolation with viper.New(): avoids global-state pollution by using per-test Viper instances.

Quick Start

Configure your Go service to use layered viper configuration by enabling AutomaticEnv with SetEnvPrefix and SetEnvKeyReplacer, then unmarshal into a typed config struct using mapstructure tags and ensure flag bindings happen before Execute().

Frequently Asked Questions about golang-spf13-viper

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

FAQPage Schema
How do I resolve Go configuration layering issues with environment variables and flags?

Go configuration layering resolves values using a fixed precedence order: Set(), bound flags, environment variables, config files, remote KV, and defaults, preventing misconfigured services. This ensures values load predictably across CLI and service deployments.

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

Map environment variables to nested keys in Go config structs by wiring SetEnvPrefix, SetEnvKeyReplacer, and AutomaticEnv together. This setup ensures keys like database.host correctly map to environment variables such as MYAPP_DATABASE_HOST.

Can I hot reload configuration files in Go applications safely?

Hot reload configuration files in Go applications safely using WatchConfig and OnConfigChange patterns. This mechanism supports reload-time validation when unmarshaling into structs via mapstructure tags, ensuring dynamic updates remain consistent.

How do I isolate Viper configuration in Go unit tests?

Isolate Viper configuration in Go unit tests by using viper.New() to create per-test instances. This approach avoids global-state pollution, ensuring deterministic test isolation when resolving layered configuration values.

What is the correct precedence order for layered config resolution in Go?

Layered config resolution in Go follows a fixed precedence order: Set() overrides, bound flags, environment variables, config files, remote KV stores, and defaults. This reasoning reduces bugs where a configured value is unexpectedly ignored.

How do I handle missing config files gracefully in Go services?

Handle missing config files gracefully in Go services by implementing ConfigFileNotFoundError handling. This allows applications to start using defaults or environment variables when optional configuration files are absent.