Golang Viper
Overview
In Golang, there are multiple packages available for handling application configuration. Among them, the most demandable package is Golang Viper which provides a complete configuration solution for an application. In this post, we will explore in detail Golang Viper and its installation.
What is Viper Library in Golang?
Golang viper is a package that helps to provide full configuration to an application in Golang with 12-factor apps. Handling and updating configuration for a large and complex program, such as creating a server application or any other application that relies heavily on user configuration manipulation, is a difficult undertaking. Furthermore, current programs are designed to run in a variety of settings, such as Docker, cloud infrastructures, and so forth.
As a result, apps should be designed to be open with limited to high configurability to preserve consistency across the deployment. External assistance in this regard is not only appreciated but also necessary for the developers involved in developing such a solution.
What does Viper Library Support in Golang?
Viper is capable of handling a wide range of configurations and formats. It supports:
- Reading JSON, TOML, YAML, HCL, envfile, and Java properties configuration files. The majority of an application's configuration information is written in this format. The majority of them are supported by Viper.
- Providing default settings configurations
- Observing environmental variables
- Remote configuration systems
- Reading flags from the command line
- Reading data from the buffer
- Putting explicit values
- We can also use viper to save any config modification we made to the file.
How to Install Viper in Golang?
Steps to install Golang Viper:
Installing the viper package is similar to installing any package in golang.
- First Once your go application is set up correctly with the required module by using go mod init which will create a go.mod file. This file will consist of or maintains a list of all packages used in the current application, Now install the Golang viper:
Check your go.mod file you will get all the configurations of the viper package.
For Example:
Golang Viper Examples
In this example we will see how to read config from the .env file using viper:
Create main.go to file and Add this code:
Create a .env file to put all the configurations:
Output
Explanation of the above code:
- Read the .env file by using the function: viper.ReadInConfig()
- Read the environment values with viper.Get("KEY"), where KEY is the variable name. For example, viper.Get("PORT") will read the PORT from the .env file.
File structure of the code:
Reading JSON Configuration in Golang with Viper
Create a sample.json file and add this code:
After that, we will add the code for reading the json from the json file sample.json.
Output
Explanation of the above code:
- In the above code, AddConfigPath() lets the viper know the location of the config file.
- This will call viper.
- SetConfigName() method to let the viper know the config file name (without extension).
- SetConfigFile(): It is used to specify the kind of configuration file (.json).
- Create a new ConfigExample struct type. This struct will keep all of the application's configuration variables that we read from files or environment variables.
- The ReadInConfig() function reads the configuration and parses the value into the ConfigExample struct.
Unmarshelling in Golang Through Viper
Viper is very helpful in both marshaling and unmarshaling. It provides two methods for this Marshal and Unmarshal
Unmarshaling through the viper example is already what we have covered in the above example where we are reading JSON and displaying output as a string.
- Marshaling means the process of converting a string into JSON.
- UnMarshaling means the process of converting JSON into a string.
Conclusion
- This article covered viper which is used to read, write, and unmarshal values from a configuration file.
- It supports a wide range of file formats, including JSON, TOML, YAML, ENV, and INI.
- It can also read values from environment variables or command-line flags.
- It allows us to set or override default values.
- It can read, write, and unmarshal values from a configuration file.
- It supports a wide range of file formats, including JSON, TOML, YAML, ENV, and so on.
- It also can read values from environment variables and command-line flags.
- It allows us to set or override default values easily.