golang-spf13-viper

Configure Go applications with Viper for layered config precedence and reloads.

2|Updated Mar 13, 2023
One-click install
npx skills add https://github.com/haipham22/golang-sample --skill golang-spf13-viper-haipham22
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-spf13-viper
Source: https://github.com/haipham22/golang-sample/tree/main/.agents/skills/golang-spf13-viper
Command: npx skills add https://github.com/haipham22/golang-sample --skill golang-spf13-viper-haipham22

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you configure Go applications with Viper correctly, avoiding the common bugs that appear when flags, environment variables, config files, defaults, and hot reloads interact.

Core Features & Use Cases

  • Layered configuration: Set up the correct precedence across flags, env vars, files, KV stores, and defaults.
  • Struct decoding: Unmarshal config into Go structs with the right mapstructure tags and decode hooks for nested values, durations, and weak typing.
  • CLI and service patterns: Handle cobra flag binding, optional config files, test isolation with per-test Viper instances, and safe hot-reload workflows.
  • Use case: You have a Go API that must read defaults from a file, override values from environment variables, accept a few CLI flags, and reload safely without corrupting live state.

Quick Start

Ask the assistant to inspect your Go config setup and produce the correct Viper initialization, binding, unmarshaling, and reload pattern for your application.

Frequently Asked Questions about golang-spf13-viper

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

FAQPage Schema
How do I set up layered configuration in Go with the correct precedence across flags, env vars, and config files?

Layered configuration in Go requires binding flags, mapping environment variables, loading config files, and setting defaults in a specific precedence order. This ensures flags override env vars, which override file values, preventing unpredictable application behavior.

Why does Viper fail to unmarshal config into my Go structs with nested values and durations?

Viper unmarshal failures often stem from missing mapstructure tags or lacking decode hooks for custom types like durations. Applying correct tags and configuring decode hooks enables accurate struct decoding for nested values and weak typing.

What is the best way to handle missing config files gracefully in a Go CLI application?

Handling missing config files gracefully requires checking file existence before loading and falling back to defaults or env vars. This prevents startup crashes in Go CLIs where config files are optional and ensures robust service initialization.

Can I use Viper with cobra for flag binding and test isolation in Go applications?

Viper works with cobra by binding command flags directly to the configuration instance. For test isolation, you can instantiate per-test Viper instances, ensuring configuration changes in one test do not leak or corrupt state in another.

How do I implement safe hot-reload behavior in Go without corrupting live configuration state?

Safe hot-reload behavior requires applying safe concurrency patterns during config reloads to avoid data races. This involves locking mechanisms or atomic swaps to ensure live application state is never corrupted while reading updated values.

What are the limitations of using Viper for Go configuration management?

Viper limitations include potential race conditions during unsafe reloads and complexities in unmarshaling weakly typed data without proper decode hooks. Careful concurrency management and struct tag configuration are required to avoid these edge case failures.