viper库的使用
-
什么是viper
viper是一个配置解决方案,方便GO语言程序处理配置信息的库,它可以处理多种格式的配置。它支持:
设置默认值
从json、toml、yaml、HCL、envfile 和 Java 属性配置文件中读取
监测配置文件的改动和重新读取配置文件 (可配置)
读取环境变量
从远程配置系统中读取配置 (etcd、consul)
从命令行标志读取
从缓冲区读取
设置显式值
-
项目地址
-
安装viper
go get github.com/spf13/viper
-
快速使用(以yaml文件为例)
-
在项目根目录中(同main.go目录下)创建一个yaml配置文件
-
Viper.AddConfigPath("./") // 文件地址 Viper.SetConfigName("config") // 文件名称 Viper.SetConfigType("yaml") // 文件名称的类型,这里配置文件是config.yaml err := Viper.ReadInConfig() // 查找并且读取配置文件信息 if err != nil { // 如果找不到文件,则会有错误信息展示 panic(fmt.Errorf("Fatal error config file: %s \n", err)) } c := Viper.AllSettings() //得到所有的配置信息 fmt.Printf("得到的配置信息:%v",c)
-
-
封装基础类
-
文件目录
-
配置文件
-
文件名:config.yaml app: APP_NAME: SHOP APP_ENV: local APP_DEBUG: true APP_URL: http://localhost:8088 APP_LOG_LEVEL: debug APP_PORT: 8088 mysql: DB_CONNECTION: mysql DB_HOST: 127.0.0.1 DB_PORT: 3306 DB_DATABASE: shop DB_USERNAME: root DB_PASSWORD: root
-
-
代码
-
文件名:config/config.go package config func Initialize() { //这里回加载当前模块下所有的init方法 }
-
文件名:config/app.go package config import "shop/tool" func init() { tool.Add("app", tool.StrMap{ "name": tool.Env("app.APP_NAME", "shop"), "env": tool.Env("app.APP_ENV", "production"), "debug": tool.Env("app.APP_DEBUG",false), "port": tool.Env("app.APP_PORT","8089"), }) }
-
文件名:tool/config.go package tool import ( "fmt" "github.com/spf13/cast" "github.com/spf13/viper" ) var Viper *viper.Viper type StrMap map[string]interface{} func init() { //初始化viper库 Viper = viper.New() Viper.AddConfigPath("./") Viper.SetConfigName("config") // name of config file (without extension) Viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name //Viper.SetConfigFile("config.yaml") // path to look for the config file in err := Viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } } func Env(name string, defaultValue ...interface{}) interface{} { if len(defaultValue) > 0{ return Get(name,defaultValue[0]) } return Get(name) } //添加到显式值中,可以优先获取 func Add(name string, configuration map[string]interface{}) { //如果某个键通过set()这个函数设置了的话,那么这个值的优先级最高。 Viper.Set(name,configuration) } //根据传递的key得到配置文件的值 func Get(key string, defaultValue ...interface{}) interface{} { value := Viper.Get(key) if value == nil { return defaultValue[0] } return value } //转换成string类型 func GetString(key string, defaultValue ...interface{}) string { return cast.ToString(Get(key, defaultValue...)) } //转换成int类型 func GetInt(key string, defaultValue ...interface{}) int { return cast.ToInt(Get(key, defaultValue...)) } //转换成uint类型 func GetUint(key string, defaultValue ...interface{}) uint { return cast.ToUint(Get(key, defaultValue...)) } //转换成int64类型 func GetInt64(key string, defaultValue ...interface{}) int64 { return cast.ToInt64(Get(key, defaultValue...)) } //转换成bool类型 func GetBool(key string, defaultValue ...interface{}) bool { return cast.ToBool(Get(key, defaultValue...)) }
-
文件名:main.go package main import ( "fmt" "shop/config" "shop/tool" ) func init() { config.Initialize() } func main() { port := tool.GetString("app.port","5005") fmt.Println(port) }
-
-