Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Viper parsing and how to load configuration

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, Xiaobian will bring you about Viper analysis and how to load configuration. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

Viper Parse & Load Configuration 1 What is Viper?

Viper is a library that makes it easy for Go applications to process configuration information. It can handle configurations in multiple formats. Features it supports:

set default values

Read configuration data from JSON, TOML, YAML, HCL, and Java properties files

Configuration file changes can be monitored, configuration files re-read

Read configuration data from environment variables

Read data from remote configuration systems and monitor them (e.g. etcd, Consul)

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 in the root directory of go, directory reference: /usr/local/go/web/config

3 Viper在Go项目中如何使用

在 config目录中,新建 config.yaml (配置)文件,内容如下:

common: database: name: test host: 127.0.0.1

3.1 在config目录中,新建 config.go (获取配置)文件,内容如下package configimport ( "fmt" "github.com/spf13/viper")func Init() (interface{},error) { //模块中供其他包调用的方法,首字母必须大写 //viper设置 配置 viper.Set("name","abc") fmt.Printf("name的值是%v\n",viper.GetString("name") ) //读取配置文件配置 viper.AddConfigPath("config") viper.SetConfigName("config") error := viper.ReadInConfig() /* 代码解析: viper.AddConfigPath("conf")用来指定yaml配置文件的路径 viper.SetConfigName("config")用来指定配置文件的名称 viper.ReadInConfig()是解析配置文件的函数,如果配置文件的路径错误获名称错误则解析失败,会报错误 viper.GetString("database.name")是用来从配置文件中根据层级关系来获取数据 最后,通过fmt.Println()对数据结果进行输出 */ if(error != nil){ panic(error) } c := viper.AllSettings() //获取所有配置 return c,nil}//获取数据库配置信息func GetDatabaseInfo() map[string]interface{} { //模块中供其他包调用的方法,首字母必须大写 return viper.GetStringMap("common.database")}//获取环境变量func GetEnvInfo(env string) string { viper.AutomaticEnv() return viper.GetString(env)}

3.2 web目录中,新建 testviper.go (加载配置)文件,内容如下:package mainimport ( "fmt" "web/config")func main() { vipConfig,error := config.Init() //vipConfig是配置 fmt.Printf("config.init error是%v\n", error) //fmt.Printf("config.init vipConfig是%v\n",vipConfig,) database := config.GetDatabaseInfo() fmt.Printf("直接获取common[database]配置是%v\n", database) fmt.Printf("直接获取common[database][host]配置是%v\n", database["host"]) //因为我们不知道 vipConfig 的下级是什么类型的数据,所以这里使用了interface{} //因此所有的类型、任意动态的内容都可以解析成 interface{}。 for key,val := range vipConfig.(map[string]interface{}){ //循环接口类型,获取配置信息 fmt.Printf("vipConfig 的key是%v val是%v\n",key,val ) switch val.(type) { //判断val的类型 case map[string]interface{}: //如果是 interface接口类型 for ke,va := rangeval.(map[string]interface{}){ //循环接口类型,获取配置信息 fmt.Printf("vipConfig 的ke是%v va是%v\n",ke,va ) switch va.(type) { //判断va的类型 case map[string]interface{}: //如果是 interface接口类型 for k,v := range va.(map[string]interface{}){ //循环接口类型,获取配置信息 fmt.Printf("vipConfig 的k是%v v是%v\n",k,v ) } } } } } //viper可以获取服务器的环境变量 GO111MODULE := config.GetEnvInfo("GO111MODULE") fmt.Printf("GO111MODULE的值是%v\n",GO111MODULE)}

3.3 使用 go run testviper.go 运行该文件即可[root@localhost web]# go run testviper.goname的值是abcconfig.init error是直接获取common[database]配置是map[host:[127.0.0.1] name:[test]]直接获取common[database][host]配置是[127.0.0.1]vipConfig 的key是common val是map[database:map[host:127.0.0.1 name:test]]vipConfig 的ke是database va是map[host:127.0.0.1 name:test]vipConfig 的k是name v是testvipConfig 的k是host v是127.0.0.1vipConfig 的key是nameval是abcGOROOT的值是on

viper支持的加载配置文件类型很多,我们从配置文件读取或者获取相关需要的数据信息,根据文件后缀名查询分割相关的配置文件类型指定操作配置的后缀名指定。上述就是小编为大家分享的Viper解析以及如何进行加载配置了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report