ScriptsApr 13, 2026·3 min read

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.

TL;DR
Viper reads Go application config from JSON, TOML, YAML, HCL, env files, and environment variables with live-reload support.
§01

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.

§02

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.

§03

How to use

  1. Install Viper with go get github.com/spf13/viper.
  2. Set config file name, path, and type, then call viper.ReadInConfig() to load.
  3. Access values with viper.GetString(), viper.GetInt(), and other typed getters.
§04

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)
}
§05

Related on TokRepo

§06

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

What config formats does Viper support?+

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.

How does Viper handle environment variables?+

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).

Can Viper watch for config file changes?+

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.

How does Viper's precedence order work?+

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.

Does Viper work with Cobra CLI framework?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets