golang-viper

Centralize Go application configuration from files, environment variables, and defaults.

Updated May 19, 2024
One-click install
npx skills add https://github.com/kettleofketchup/dotfiles --skill golang-viper
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-viper
Source: https://github.com/kettleofketchup/dotfiles/tree/main/.claude/skills/golang-viper
Command: npx skills add https://github.com/kettleofketchup/dotfiles --skill golang-viper

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go applications often need flexible configuration sources. Viper unifies reading from config files, environment variables, defaults, and remote stores, simplifying maintainable configuration management.

Core Features & Use Cases

  • Unified config sources: read YAML/JSON/TOML files, bind environment variables, apply defaults, and support remote key/value stores.
  • Environment prioritization and nested keys: overrides from env vars withPrefix and key replacers, mapping to nested Go structs.
  • Easy integration: unmarshall into Go structs, watch for changes, and compose multiple config files for secrets or overrides.

Quick Start

  • Create a config file (for example config.yaml) and place keys under server.host and server.port.
  • In your Go code, initialize Viper with a config name and paths, set a config type if needed, load the config, and access values using viper.GetString("server.host") or viper.GetInt("server.port").
  • Optional: enable environment variable overrides by setting a prefix and enabling AutomaticEnv, plus set defaults before reading in config.

Frequently Asked Questions about golang-viper

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

FAQPage Schema
How do I manage Go application configuration across environment variables and config files?

You can manage Go application configuration by using Viper to unify reading from YAML, JSON, and TOML config files alongside environment variables and defaults. This centralizes configuration management into a single source of truth.

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

To map nested environment variables to Go structs in Viper, you set an environment prefix and enable AutomaticEnv with key replacers. You then use unmarshalling to map these overridden nested keys directly into your Go structs.

Can I override YAML config values with environment variables in a Go app?

Yes, you can override YAML config values with environment variables in a Go app by enabling Viper's environment prioritization. Setting a prefix and enabling AutomaticEnv ensures environment variables take precedence over file values.

What is the best way to read multiple config files for secrets and overrides in Go?

The best way to read multiple config files for secrets and overrides in Go is composing multiple config files with Viper. This approach allows you to merge base configurations with separate files containing secrets or environment overrides.

Does Viper support hot reloading configuration changes at runtime in Go?

Yes, Viper supports hot reloading configuration changes at runtime in Go applications. You can watch for changes in your config files, allowing your Go application to dynamically update settings without requiring a restart.

What do I need to set up before reading config values with Viper in Go?

Before reading config values with Viper in Go, you need the Viper library, a config file in YAML, JSON, or TOML format, an optional environment prefix, and basic Go tooling to initialize config paths and read values.