It is recommended to read the official documentation
The role of the viper library
viper is suitable for processing configuration files in multiple formats (such as YAML, JSON, TOML, etc.) in go applications.
It supports:
- set default
- Read from JSON, TOML, YAML, HCL, envfile and Java property configuration files
- Watch and re-read configuration files in real time (optional)
- read from environment variable
- Read from a remote configuration system (etcd or Consul), and watch for changes
- read from command line flags
- read from buffer
- set explicit value
Viper can be thought of as a registry for all application configuration needs.
use
- Install
go get github.com/spf13/viper
- Create a
config.yaml
configuration file named YAML format, for example:
database:
host: localhost
port: 5432
user: myuser
password: mypassword
- Write the main function
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigFile("config.yaml") // 设置配置文件名
viper.SetConfigType("yaml") // 设置配置文件类型
viper.AddConfigPath(".") // 设置配置文件路径
err := viper.ReadInConfig() // 读取配置文件
if err != nil {
panic(fmt.Errorf("读取配置文件失败: %s \n", err))
}
dbHost := viper.GetString("database.host") // 读取数据库主机名
dbPort := viper.GetInt("database.port") // 读取数据库端口号
dbUser := viper.GetString("database.user") // 读取数据库用户名
dbPassword := viper.GetString("database.password") // 读取数据库密码
fmt.Printf("%v %v %v %v ", dbHost, dbPort, dbUser, dbPassword)
// 使用配置信息连接数据库...
}
- The overall structure is as follows
├─go.mod
├─config
└─main.go
viper also has some important functions
viper.SetDefault(key string, value interface{})
: Set the default value. If no key-value pair is found when reading the configuration file, the default value is used.
viper.SetDefault("database.host", "localhost")
viper.SetDefault("database.port", 5432)
viper.GetString(key string) string
: Gets the value of a configuration parameter of type string.
dbHost := viper.GetString("database.host")
viper.GetInt(key string) int
: Gets the value of a configuration parameter of type integer.
dbPort := viper.GetInt("database.port")
viper.GetBool(key string) bool
: Gets the value of a configuration parameter of type Boolean.
debugMode := viper.GetBool("debug")
viper.GetDuration(key string) time.Duration
: Gets the value of a configuration parameter of type Duration.
timeout := viper.GetDuration("timeout")
viper.GetStringSlice(key string) []string
: Gets the value of a configuration parameter of string slice type.
allowedIPs := viper.GetStringSlice("security.allowed_ips")
viper.Set(key string, value interface{})
: Sets the value of the configuration parameter.
viper.Set("database.host", "db.example.com")
viper.WatchConfig()
: Watch for changes to the configuration file and reload the configuration file.
viper.WatchConfig()
viper.ReadInConfig()
: Read and parse the specified configuration file.
err := viper.ReadInConfig()
if err != nil {
fmt.Printf("读取配置文件失败: %s\n", err)
}
These functions are some of the most commonly used functions in Viper, but there are many other useful functions such as viper.AllSettings()
, viper.IsSet()
, viper.Unmarshal()
etc. Using Viper, you can easily read and manage your application's configuration information without manually parsing and processing configuration files.