Search⌘ K

Introduction to Viper

Explore Viper's capabilities to manage configuration in Go programs, including its integration with Cobra, support for multiple config sources, nested access, and unmarshalling into structs. Learn how Viper handles type conversions, setting precedence, and error handling to build robust command-line applications.

Viper supports many modes of configuration and it integrates with Cobra. In particular Viper supports configuration files in many formats, environment variables, command-line flags and remote key value stores. It also provides advanced access mechanisms to your configuration like nested access and marshaling from Go structs.

Overview

Viper aims to address all your configuration needs for Go programs. It is designed to support any use case from command-line programs all the way to 12-factor distributed apps.

It is called Viper because it is a companion to Cobra. Of course, it has a cool logo

widget

Let’s look at Viper API, data structures and the various mechanisms and modes it brings to the table.

Foundations of Viper

Viper presents a pretty simple interface. Viper manages settings and you can get settings via a variety of GetXXX() functions for specific types. For example:

import
...