“This is the 18th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.”
Introduction to the
Continuing with SPF13’s project Viper is a library for reading configuration files
Viper website: github.com/spf13/viper
The website claims to be able to read all types of configuration and format, but also has hot load configuration function, very powerful
The characteristics of
Supported configuration types and formats:
- Support for reading JSON, TOML, YAML, HCL, envfile and Java properties configuration file
- Reading environment variables is supported
- Supports reading configuration of remote components (ETCD or Consul)
- Support for Go command-line arguments (flags)
- Supports setting default and fixed values
- The configuration is case insensitive
Configuration format priority:
- explicit call to
Set
- flag
- env
- config
- key/value store
- default
use
This article will not demonstrate all configuration types and all functions, but only the parts that you often use
The installation
Install based on go Model
go get -u github.com/spf13/viper
Copy the code
Introduced in code
import "github.com/spf13/viper"
Copy the code
Example 1(simply read configuration and output)
Start by writing a yaml configuration file called dev.yaml
serverPort: 8808
logLevel: "INFO"
mysql:
host: "127.0.0.1"
port: 3306
database: "viper"
user: "viper"
password: "viper33"
Copy the code
Write the demo, read the configuration file, and print it
By default, the path, type, and name of viper are not configured
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main(a) {
// Define the profile name
viper.SetConfigName("dev")
// Define the configuration file type
viper.SetConfigType("yaml")
// Define the configuration path
viper.AddConfigPath(".")
err := viper.ReadInConfig()
iferr ! =nil {
panic(fmt.Errorf("Configuration file not found, error log :: %w \n", err))
}
// Read the value and print it out
fmt.Printf("serverPort: %v\n",viper.Get("serverPort"))
fmt.Printf("logLevel: %v\n",viper.Get("logLevel"))
fmt.Println("mysql info:",viper.Get("mysql.host"),
viper.Get("mysql.port"),viper.Get("mysql.database"),
viper.Get("mysql.user"),viper.Get("mysql.password"))}Copy the code
Results the following
Example 2(hot loading and defaults)
Example 1 is used as an example for the configuration file
To set the default value, call SetDefault with k/v, but the default value is the lowest priority. If the configuration file or environment variable has the same key, the default value is invalidated
Note that, after modifying the configuration, must Ctrl+ C save, otherwise the configuration will not change
package main
import (
"fmt"
"github.com/spf13/viper"
"time"
)
func main() {
// Define the profile name
viper.SetConfigName("dev")
// Define the configuration file type
viper.SetConfigType("yaml")
// Define the configuration path
viper.AddConfigPath(".")
// Define default values
viper.SetDefault("env"."production")
err := viper.ReadInConfig()
iferr ! = nil { panic(fmt.Errorf("Configuration file not found, error log :: %w \n", err))
}
// Monitor configuration file changes, automatic hot loading
viper.WatchConfig()
// Read the default values taken from the definition
fmt.Printf("env: %v\n",viper.Get("env"))
// Test configuration changes
fmt.Printf("logLevel: %v\n",viper.Get("logLevel"))
time.Sleep(8 * time.Second)
fmt.Printf("logLevel: %v\n",viper.Get("logLevel"))}Copy the code
Run as follows
Remote configuration
Panic: Remote Configurations Error: No Files Found!
But there is a little elder brother blog got another set of solutions, portal: blog.huoding.com/2020/08/10/…
Nacos support
There is also a library about viper’s support for nacOS configuration, which is very good github.com/yoyofxteam/…
reference
github.com/spf13/viper