August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn how to read JSON configuration files with the viper package. The name of the utility is readJSON.go and it is going to be presented in three parts. The first part of readJSON.go is as follows:
package main
import (
"fmt"
"github.com/spf13/viper"
)
The second part of readJSON.go contains the following code:
func main() {
viper.SetConfigType("json")
viper.SetConfigFile("./myJSONConfig.json")
fmt.Printf("Using config: %s\n", viper.ConfigFileUsed())
viper.ReadInConfig()
This is where the parsing of the configuration file takes place. Please note that the filename of the JSON configuration file is hardcoded inside readJSON.go using the viper.SetConfigFile("./myJSONConfig.json") ...
Read now
Unlock full access