viper
viper是适用于go应用程序的完整配置解决方案。它被设计用于在应用程序中工作,并且可以处理所有类型的配置需求和格式。它支持以下特性:
- 设置默认值
- 从json、toml、yaml、hcl、envfile和java properties 格式的配置文件读取配置信息
- 实时监控和重新读取配置文件(可选)
- 从环境变量中读取
- 从远程配置系统(etcd或consul)读取并监控配置变化
- 从命令行参数读取配置
- 从buffer读取配置
- 显式配置值
安装
go get github.com/spf13/viper
使用
在项目根目录创建config文件夹,在文件夹中创建local.yaml
servername: 'myserver' mysql: name: 'mysql'
在main.go中读取配置文件的信息
package main import ( "log" "github.com/spf13/viper" ) func main() { v := viper.new() // 设置文件名称 v.setconfigfile("./config/local.yaml") if err := v.readinconfig(); err != nil { log.panicln("读取配置文件失败") } servername := v.get("servername") mysql := v.get("mysql.name") log.println(servername, mysql) }
使用结构体映射yaml
修改配置文件
name: '服务名' port: 8080
使用
package main import ( "log" "github.com/spf13/viper" ) type serverconfig struct { name string `mapstructure:"name"` port int `mapstructure:"port"` } func main() { cfg := serverconfig{} v := viper.new() // 设置文件名称 v.setconfigfile("./config/local.yaml") if err := v.readinconfig(); err != nil { log.panicln("读取配置文件失败") } if err := v.unmarshal(&cfg); err != nil { log.panicln(err) } log.println(cfg) }
输出
{服务名 8080}
结构体嵌套读取配置
配置文件
name: '服务名' port: 8080 mysql: host: '127.0.0.1' port: 3306
读取配置
package main import ( "log" "github.com/spf13/viper" ) type serverconfig struct { name string `mapstructure:"name"` port int `mapstructure:"port"` mysqlconfig mysqlconfig `mapstructure:"mysql"` // 嵌套的配置 } type mysqlconfig struct { host string `mapstructure:"host"` port int `mapstructure:"port"` } func main() { cfg := serverconfig{} v := viper.new() // 设置文件名称 v.setconfigfile("./config/local.yaml") if err := v.readinconfig(); err != nil { log.panicln("读取配置文件失败") } if err := v.unmarshal(&cfg); err != nil { log.panicln(err) } log.println(cfg) }
输出
{服务名 8080 {127.0.0.1 3306}}
根据不同环境读取不同配置文件
在实际开发工作中,一般有不同的开发环境,会涉及到读取不同的配置文件
这会运用的golang的flag包
package main import ( "flag" "log" ) func main() { env := flag.string("env", "local", "运行环境请输入local dev test prod") flag.parse() log.println("当前环境是" + *env) }
运行
go run .\main.go -help
输出
-env string
运行环境请输入local dev test prod (default "local")
这样的话运行
go run .\main.go -env dev
输出
当前环境是dev
利用flag读取不同环境的配置文件
在项目更目录新建config文件夹
并创建4个配置文件 local.yaml dev.yaml test.yaml prod.yaml
其中dev.yaml中
name: 'dev服务名' port: 8080 mysql: host: '127.0.0.1' port: 3306
在main.go中
package main import ( "flag" "fmt" "log" "github.com/spf13/viper" ) type serverconfig struct { name string `mapstructure:"name"` port int `mapstructure:"port"` mysqlconfig mysqlconfig `mapstructure:"mysql"` // 嵌套的配置 } type mysqlconfig struct { host string `mapstructure:"host"` port int `mapstructure:"port"` } func main() { env := flag.string("env", "local", "运行环境请输入local dev test prod") flag.parse() log.println("当前环境是" + *env) cfg := serverconfig{} v := viper.new() var path string = fmt.sprintf("./config/%s.yaml", *env) // 设置文件名称 v.setconfigfile(path) if err := v.readinconfig(); err != nil { log.panicln("读取配置文件失败") } if err := v.unmarshal(&cfg); err != nil { log.panicln(err) } log.println(cfg) }
运行
go run .\main.go -env dev
输出
当前环境是dev
{dev服务名 8080 {127.0.0.1 3306}}
到此这篇关于golang读取yaml配置文件的方法实现的文章就介绍到这了,更多相关golang读取yaml内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论