Claude-skill-registry golang-viper
Go Viper configuration library (github.com/spf13/viper). Use when working with config file reading (SetConfigName, AddConfigPath, ReadInConfig), setting defaults, environment variable binding (SetEnvPrefix, AutomaticEnv, BindEnv), unmarshaling to structs (Unmarshal, UnmarshalKey, mapstructure tags), or any spf13/viper configuration management.
install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/golang-viper" ~/.claude/skills/majiayu000-claude-skill-registry-golang-viper && rm -rf "$T"
manifest:
skills/data/golang-viper/SKILL.mdsource content
Golang Viper Configuration
Viper is Go's most popular configuration library. It handles config files, environment variables, defaults, and remote config stores with a unified API.
Config Precedence (highest to lowest)
- explicit runtime overridesviper.Set()- Command-line flags (via pflag)
- Environment variables
- Config files
- Remote key/value stores (etcd, Consul)
- defaultsviper.SetDefault()
Quick Reference
// Basic setup viper.SetConfigName("config") // filename without extension viper.SetConfigType("yaml") // explicit format viper.AddConfigPath(".") // search current dir viper.AddConfigPath("$HOME/.myapp") // search home dir // Set defaults before reading viper.SetDefault("server.port", 8080) // Read config if err := viper.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { // Config file not found; use defaults } else { return fmt.Errorf("config error: %w", err) } } // Get values host := viper.GetString("server.host") port := viper.GetInt("server.port") debug := viper.GetBool("debug") // Unmarshal to struct var config Config viper.Unmarshal(&config) viper.UnmarshalKey("server", &config.Server)
Key Behaviors
- Keys are case-insensitive:
andserver.PORT
are the sameserver.port - Env vars ARE case-sensitive:
differs fromAPP_PORTapp_port - Nested keys use dots:
maps to YAMLserver.hostserver: { host: ... } - Not thread-safe: wrap concurrent access with mutex
References
Detailed documentation for each feature area:
- Config files, paths, defaults, reading, multiple configsreferences/core-config.md
- Env binding, prefixes, key replacers, AutomaticEnvreferences/environment-vars.md
- Structs, mapstructure tags, type getters, custom typesreferences/unmarshaling.md
- Watching changes, remote config, writing configsreferences/advanced-features.md
Common Patterns
Multiple Config Files (config + secrets)
// Main config viper.SetConfigName("config") viper.ReadInConfig() // Separate secrets file secretsViper := viper.New() secretsViper.SetConfigName("secrets") secretsViper.AddConfigPath(".") secretsViper.ReadInConfig()
Environment Variable Override
viper.SetEnvPrefix("MYAPP") // MYAPP_SERVER_PORT viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv()