DEV Community

Cover image for Managing Configurations in Golang with Viper
luthfisauqi17
luthfisauqi17

Posted on

2

Managing Configurations in Golang with Viper


Managing configurations in a Golang application can be tricky, especially when dealing with different environments, file formats, and environment variables.

This is where Viper comes in!

Viper is a powerful Go library that simplifies configuration management by supporting:

✅ Multiple file formats (YAML, JSON, TOML, etc.)
✅ Environment variables for dynamic settings
✅ Command-line flags
✅ Default values for missing configs

In this guide, we’ll explore how to use Viper to handle configurations efficiently in your Go applications.

Installing Viper in Golang

Before using Viper, install it in your Go project by running:

go get github.com/spf13/viper
Enter fullscreen mode Exit fullscreen mode

Then, create a main.go file and import the package:

package main

import (
    "fmt"
    "github.com/spf13/viper"
)
Enter fullscreen mode Exit fullscreen mode

Now, let’s see how to load configurations from a file!

Loading Configuration from a File

Viper can read configuration from multiple file formats like YAML, JSON, and TOML.

Let’s create a config.yaml file:

app_name: "Sample Go Application"
port: 8080
debug: true
Enter fullscreen mode Exit fullscreen mode

Now, in main.go, load this configuration:

func main() {
    viper.SetConfigName("config") // Name of the config file (without extension)
    viper.SetConfigType("yaml")   // File format
    viper.AddConfigPath(".")      // Look for config in the current directory

    // Read the config file
    if err := viper.ReadInConfig(); err != nil {
        fmt.Printf("Error reading config file: %v\n", err)
        return
    }

    // Access config values
    fmt.Println("App Name:", viper.GetString("app_name"))
    fmt.Println("Port:", viper.GetInt("port"))
    fmt.Println("Debug Mode:", viper.GetBool("debug"))
}
Enter fullscreen mode Exit fullscreen mode

Run the Program

go run main.go
Enter fullscreen mode Exit fullscreen mode

You should see the following output:

App Name: Sample Go Application
Port: 8080
Debug Mode: true
Enter fullscreen mode Exit fullscreen mode

Setting Default Values

What if a configuration value is missing? Viper allows setting default values to avoid errors.

viper.SetDefault("port", 8080)
Enter fullscreen mode Exit fullscreen mode

If a config file or environment variable is missing, Viper will fall back to the default value.

Using JSON Instead of YAML

Viper supports various file formats. If you prefer JSON, just update the config type:

viper.SetConfigType("json")
Enter fullscreen mode Exit fullscreen mode

And create a config.json file:

{
  "app_name": "Sample Go Application",
  "port": 8080,
  "debug": true
}
Enter fullscreen mode Exit fullscreen mode

Viper will handle it seamlessly!


There you go, that is how you can use Viper to manage configuration in Golang. Thank you for reading, and have a nice day!

Top comments (0)