Viper parsing & loading configuration
1. What is Viper
Viper is a library that facilitates go language applications to process configuration information. It can handle configurations in a variety of formats. Features it supports:
- Set defaults
- Read configuration data from JSON, toml, yaml, HCl, and Java properties files
- You can monitor the changes of the configuration file and re read the configuration file
- Read configuration data from environment variables
- Read data from the remote configuration system and monitor them (e.g. etcd, consult)
- Read configuration from command parameters
- Read from buffer
- Call function to set configuration information
2. Install Viper
go get github.com/spf13/viper
Create a new web / config directory under the root directory of go. The directory reference is: / usr / local / go / Web / config
3. How viper is used in go project
In the config directory, create a new config Yaml (configuration) file, as follows:
common:
database:
name: test
host: 127.0.0.1
3.1} in the config directory, create a new config Go (get configuration) file, as follows
package config
import (
"fmt"
"github.com/spf13/viper"
)
Func init() (interface {}, error) {// the first letter of the method in the module that can be called by other packages must be capitalized
//Viper settings configuration
viper.Set("name","abc")
fmt. Printf ("the value of name is% v \ n", viper.getstring ("name"))
//Read profile configuration
viper.AddConfigPath("config")
viper.SetConfigName("config")
error := viper.ReadInConfig()
/*
Code parsing:
viper. Addconfigpath ("conf") is used to specify the path of yaml configuration file
viper. Setconfigname ("config") is used to specify the name of the configuration file
viper. Readinconfig() is a function to parse the configuration file. If the path of the configuration file is wrong and the name is wrong, the parsing fails and an error will be reported
viper. GetString ("database. Name") is used to obtain data from the configuration file according to the hierarchical relationship
Finally, through FMT Println() outputs the data results
*/
if(error != nil){
panic(error)
}
c := viper. Allsettings() // get all configurations
return c,nil
}
//Get database configuration information
Func getdatabaseinfo() map [string] interface {} {// the method in the module that can be called by other packages. The first letter must be capitalized
return viper.GetStringMap("common.database")
}
//Get environment variables
func GetEnvInfo(env string) string {
viper.AutomaticEnv()
return viper.GetString(env)
}
3.2. In the web directory, create a new testviper Go (load configuration) file, as follows:
package main
import (
"fmt"
"web/config"
)
func main() {
vipConfig,error := config. Init() // vipconfig is the configuration
fmt. Printf ("config.init error is% v \ n", error)
//fmt. Printf ("config.init vipconfig is% v \ n", vipconfig,)
database := config.GetDatabaseInfo()
fmt. Printf ("get common [database] configuration directly is% v \ n", database)
fmt. Printf ("get common [database] [host] configuration directly is% v \ n, database [" host "])
//Because we don't know what type of data is subordinate to vipconfig, we use interface {}
//Therefore, all types and any dynamic content can be resolved into interface {}.
for key,val := range vipConfig. (map [string] interface {}) {// loop the interface type. Get the configuration information
fmt. Printf ("vipconfig's key is% v, Val is% v \ n", key, Val)
Switch val. (type) {// judge the type of val
Case map [string] interface {}: // if it is an interface type
For Ke, VA: = range val. (map [string] interface {}) {// loop interface type. Get configuration information
fmt. Printf ("Ke of vipconfig is% v, VA is% v \ n", Ke, VA)
Switch va. (type) {// judge the type of VA
Case map [string] interface {}: // if it is an interface type
For K, V: = range va. (map [string] interface {}) {// loop interface type. Get configuration information
fmt. Printf ("K of vipconfig is% v, V is% v \ n", K, V)
}
}
}
}
}
//Viper can get the environment variables of the server
GO111MODULE := config.GetEnvInfo("GO111MODULE")
fmt. Printf ("the value of go111module is% v \ n", go111module)
}
3.3} use go run testpipe Go run the file
[[email protected] web]# go run testviper.go
The value of name is ABC
config. Init error yes
Get the common [database] configuration directly as map [host: [127.0.0.1] name: [test]]
The configuration of common [database] [host] obtained directly is [127.0.0.1]
The key of vipconfig is common and val is map [Database: map [host: 127.0.0.1 Name: test]]
The Ke of vipconfig is database and VA is map [host: 127.0.0.1 Name: test]
K of vipconfig is name and V is test
K of vipconfig is host V is 127.0.0.1
The key of vipconfig is name and val is ABC
The value of goroot is on
4. Summary
Viper supports many types of loading configuration files. We read or obtain the relevant required data information from the configuration file, query and split the relevant configuration file types according to the file suffix, and specify the suffix of the operation configuration.
This article is written by the blogger Liu Yusheng
GitHub address: https://github.com/vpc123