Find your next open-source project

Search by name, purpose, or keyword, or paste a GitHub URL. Start with / to search agent skills (e.g. /pdf)

spf13/viper

Developer Tools

Go library that merges config from files, env vars, flags, and remote stores into one case-insensitive key space.

30,605
2,354
Go
MIT
12 years old

What it does

Viper is a configuration library for Go applications. It reads defaults, config files (JSON, TOML, YAML, INI, envfile, Java properties), environment variables, pflag command-line flags, io.Reader buffers, and remote key/value stores such as Etcd, Consul, Firestore, and NATS, then merges them into a single set of keys. Precedence, highest first: explicit Set, flags, environment variables, config files, remote stores, defaults. Keys are case-insensitive.

It is aimed at Go developers who need layered configuration without writing source-priority logic themselves. It pairs with Cobra for CLI applications, and the README lists Hugo, Docker Notary, doctl, Coder, and Vitess among users. It also supports live file watching, remote config watching, aliases for key refactoring, and unmarshaling into structs via mapstructure.

Quick start

Requires Go 1.23 or later (per the README badge) and Go Modules. Add the dependency:

go get github.com/spf13/viper

Set a name and search paths, then read the first matching file:

viper.SetConfigName("config")
viper.AddConfigPath("/etc/appname/")
viper.AddConfigPath("$HOME/.appname")
viper.AddConfigPath(".")

err := viper.ReadInConfig()
if err != nil {
    panic(fmt.Errorf("fatal error config file: %w", err))
}

After this, viper.GetString("key") and related typed getters return merged values. To load remote config, blank-import github.com/spf13/viper/remote, call AddRemoteProvider, and set the config type explicitly because there is no file extension.

Constraints and known issues

  • One Viper instance reads one config file; multiple paths are searched, not merged.
  • Configuration values are not deep-merged: overriding a complex value replaces it entirely.
  • Keys are forced to lowercase, which can silently merge differently-cased keys from JSON/YAML/TOML. This is a documented design choice and has been criticized by users.
  • A Viper instance is not safe for concurrent reads and writes; callers must synchronize access.
  • The global singleton is discouraged and may be deprecated; prefer viper.New() and pass the instance.
  • Empty environment variables are treated as unset unless AllowEmptyEnv is used; environment variables are case-sensitive.
  • The project prioritizes backward compatibility and stability; features may be deferred to Viper v2.
  • MIT licensed.

HotRepo's take

The weekly Go snapshot for 2026-12-04 records 30,604 stars and 60 stars gained that week, with a Go-list best position of 13. No all-language daily rank is recorded. The 2022 Hacker News threads brought substantive critiques: one commenter called its dependency footprint heavy for a config package (16 direct, 39 transitive dependencies, go.mod) and argued the standard library plus glue suffices for many apps comment; a reply noted that layered source override is where the complexity pays off reply. Another thread contains a maintainer of an alternative library describing silent lowercase merging and hardcoded dependencies as reasons to avoid Viper comment, referencing a PR (7). These are opinions from 2022, but they point to the same trade-off visible in the README: Viper optimizes for breadth of sources, not minimal footprint or spec-exact key handling.

Written by AI from the project's public information and HotRepo's records, updated . The project's own docs are the source of truth.

Hacker News

Viper – Go Configuration with Fangs
▲ 10 · 3 comments · May 21, 2022
Viper: Go Configuration with Fangs
▲ 3 · 0 comments · August 29, 2022

You might also like

  1. jdx/miseA single CLI for installing project dev tools, loading env vars, and running tasks through one mise.toml.Rust35,796