Introduction to Viper

In this lesson, you'll meet Viper, the configuration library from the creator of Cobra.

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 (
	"fmt"
	"github.com/spf13/viper"
)

func main()
...