Featured image of post Viper

Viper

Go语言配置管理神器

GitHub API Docs Docs Docs

读取配置文件

远程读取

导入包_ "github.com/spf13/viper/remote"

consul

如果开启了ACL,则要设置环境变量CONSUL_HTTP_TOKEN

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// 设置存储配置文件的远程主机地址
viper.AddRemoteProvider("consul", "endpoint", "path")
// 示例
// viper.AddRemoteProvider("consul", "test.com", "config/test")

// 设置配置文件的类型,方便对获取到的value解码
// 支持的扩展名有 "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
viper.SetConfigType("type")
// 示例
// viper.SetConfigType("yaml")

// 读取远程配置文件
err:=viper.ReadRemoteConfig()
if err != nil {
	panic(err)
}

解析匿名结构体

1
2
3
err := c.viper.UnmarshalKey(key, rawVal, func(decoderConfig *mapstructure.DecoderConfig) {
   decoderConfig.Squash = true
})

lumberjack

日志分割

GitHub

渝ICP备2022001449号
本站总访问量