golang解析yaml文件

您所在的位置:网站首页 golang读取yaml golang解析yaml文件

golang解析yaml文件

2023-03-25 10:13| 来源: 网络整理| 查看: 265

golang解析yaml文件

  yaml 文件是目前最常用的配置文件,使用go语言编写代码和工具时,也会用到yaml文件,将服务配置及中间件等信息定义到yaml文件中,那么如何将yaml文件中定义的信息读取到代码中进行使用呢?此处使用yaml包和viper包来解析配置文件,后续可根据实际场景来选用。

一、yaml包

  yaml包这里使用"gopkg.in/yaml.v2",以下为示例代码:

1、定义yaml配置文件

#config.yamllisten: ':8050' secret_key: 123 bowls: false strslice: - 'asd' - 'qwe' auth: - id: 'root' password: '456' roles: - 'admin' - id: 'cjs' password: '123' roles: - 'one' keymap: a: 'xml' b: 'opi' c: 'edg'

2、主程序

package main import ( "fmt" "io/ioutil" "os" v2 "gopkg.in/yaml.v2" ) var ( cfgFile = "config.yaml" ) type user struct { #定义user结构体 Id string `yaml:"id"` Password string `yaml:"password"` Roles []string `yaml:"roles"` } type Config struct { #定义Config结构体 Listen string `yaml:"listen"` SecretKey int `yaml:"secret_key"` Boll bool `yaml:"bowls"` StrSlice []string `yaml:"strslice"` Auth []user `yaml:"auth"` KeyMap map[string]string `yaml:"keymap"` } func test() { data, err := ioutil.ReadFile(cfgFile) if err != nil { fmt.Printf("err: %v\n", err) return } conf := new(Config) if err := v2.Unmarshal(data, conf); err != nil { #使用yaml.Unmarshal将yaml文件中的信息反序列化给Config结构体 fmt.Printf("err: %v\n", err) return } fmt.Printf("conf: %v\n", conf) fmt.Printf("conf.SecretKey: %v\n", conf.SecretKey) #通过结构体语法取值 out, err := v2.Marshal(conf) #序列化为yaml格式文件 if err != nil { fmt.Printf("err: %v\n", err) return } fmt.Printf("out: %v\n", string(out)) return } func main() { test() }

3、输出结果

conf: &{:8050 123 false [asd qwe] [{root 456 [admin]} {cjs 123 [one]}] map[a:xml b:opi c:edg]} conf.SecretKey: 123 out: listen: :8050 secret_key: 123 bowls: false strslice: - asd - qwe auth: - id: root password: "456" roles: - admin - id: cjs password: "123" roles: - one keymap: a: xml b: opi c: edg

二、viper包

  此处使用"github.com/spf13/viper"包,常和pflag和cobra结合使用。

1、配置文件

#config.yamlport: 8081 version: "v0.0.4" mysql: host: "127.0.0.1" port: 3306 dbname: "sql_demo"

2、主程序

package main import ( "fmt" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" ) type Config struct { Port int `mapstructure:"port"` #此处 Version string `mapstructure:"version"` MysqlConfig `mapstructure:"mysql"` } type MysqlConfig struct { Host string `mapstructure:"host"` Port int `mapstructure:"port"` DbName string `mapstructure:"dbname"` } func main() { //设置默认值 viper.SetDefault("fileDir", "./") viper.SetDefault("LayoutDir", "layouts") //读取配置文件 viper.SetConfigName("config") //配置文件名称(无扩展名) viper.SetConfigType("yaml") //如果配置文件中没有扩展名,则需要配置此项 viper.AddConfigPath("/etc/appname") //查找配置文件所在的路径 viper.AddConfigPath("$HOME/.appname") //多次调用以添加多个搜索路径 viper.AddConfigPath("./") //还可以在工作目录中查找配置 //读取配置文件 err := viper.ReadInConfig() //查找并读取yaml配置文件 if err != nil { panic(fmt.Errorf("Fatal error config:%s \n", err)) } fmt.Println("viper.LayoutDir:", viper.Get("LayoutDir")) //获取默认值 //将配置文件解析到结构体 var c Config if err := viper.Unmarshal(&c); err != nil { #反序列配置到结构体 fmt.Printf("viper.Unmarshall failed,err:%v\n", err) return } fmt.Printf("c:%#v\n", c) fmt.Printf("c.Version: %v\n", c.Version) #使用结构体语法取值 fmt.Printf("c.MysqlConfig.Host: %v\n", c.MysqlConfig.Host) fmt.Printf("viper.Get(\"mysql\"): %v\n", viper.Get("mysql")) }

 

  



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3