Viper — Complete Configuration Solution for Go
Viper is the most popular configuration library for Go applications. It reads config from JSON, TOML, YAML, HCL, envfile, and Java properties files, supports environment variables, remote config systems, and live watching for config changes.
What it is
Viper is the most widely adopted configuration library for Go applications. It supports reading configuration from JSON, TOML, YAML, HCL, envfile, and Java properties formats. Viper also reads from environment variables, command-line flags, remote config systems (etcd, Consul), and provides live watching for config file changes.
Viper is used by Go developers building applications that need flexible configuration management across multiple environments (development, staging, production) without hardcoding values.
How it saves time or tokens
Viper eliminates the need to write custom config parsing code for each format. Setting defaults, overriding with environment variables, and binding CLI flags all work through a single API. The live-watching feature reloads config files without restarting the application, which is valuable for long-running services. Viper handles config merging across multiple sources with a clear precedence order.
How to use
- Install Viper with
go get github.com/spf13/viper. - Set config file name, path, and type, then call
viper.ReadInConfig()to load. - Access values with
viper.GetString(),viper.GetInt(), and other typed getters.
Example
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.SetDefault("port", 8080)
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("No config file found: %v\n", err)
}
viper.AutomaticEnv()
port := viper.GetInt("port")
fmt.Printf("Server starting on port %d\n", port)
}
Related on TokRepo
- AI tools for coding -- developer libraries and tools
- Automation tools -- configuration and automation workflows
Common pitfalls
- Viper uses a global singleton by default; for library code or multiple configs, create isolated instances with
viper.New(). - Environment variable binding is case-insensitive and uses underscores for nested keys (e.g., DATABASE_HOST maps to database.host), which can cause unexpected collisions.
- The WatchConfig feature requires fsnotify and may not work reliably on all filesystems, particularly network-mounted volumes.
Frequently Asked Questions
Viper supports JSON, TOML, YAML, HCL, envfile, and Java properties file formats. It auto-detects the format based on file extension or the explicitly set config type. You can also read configuration from io.Reader for custom sources.
Call viper.AutomaticEnv() to automatically bind environment variables. Viper maps nested config keys to environment variables using underscores as separators. You can set a prefix with viper.SetEnvPrefix() to namespace your variables (e.g., MYAPP_PORT).
Yes. Call viper.WatchConfig() to enable file watching via fsnotify. You can register a callback with viper.OnConfigChange() to react to changes. This allows live configuration updates without application restarts.
Viper merges configuration from multiple sources with this precedence (highest to lowest): explicit Set calls, flags, environment variables, config file, key/value store, and defaults. Higher-precedence sources override lower ones.
Yes. Viper and Cobra (by the same author, Steve Francia) are designed to work together. Cobra handles CLI flag parsing and Viper handles configuration. You bind Cobra flags to Viper with viper.BindPFlag() so flags and config files share the same keys.
Citations (3)
- Viper GitHub— Viper configuration library for Go
- Viper README— Supports JSON, TOML, YAML, HCL, envfile, properties
- Cobra GitHub— Cobra and Viper integration
Related on TokRepo
Discussion
Related Assets
doctest — The Fastest Feature-Rich C++ Testing Framework
doctest is a single-header C++ testing framework designed for minimal compile-time overhead and maximum speed.
Chai — BDD/TDD Assertion Library for Node.js
Chai is a flexible assertion library for Node.js and browsers that supports expect, should, and assert styles.
Supertest — HTTP Assertion Library for Node.js APIs
Supertest provides a high-level API for testing HTTP servers in Node.js with fluent assertion chaining.