美文网首页
go使用yaml解析

go使用yaml解析

作者: Doter | 来源:发表于2020-10-24 19:43 被阅读0次

golang解析yaml遇到的坑

解析文件

//Config 为系统全局配置
type Config struct {
    Server struct {
        Port int    `yaml:"port"`
        Code string `yaml:"code"`
    }
}

//GetConfig 获取配置数据
func GetConfig(filePath string) {
    config := Config{}
    content, err := ioutil.ReadFile(filePath)
    if err != nil {
        log.Fatalf("解析config.yaml读取错误: %v", err)
    }

    fmt.Println(string(content))
    fmt.Printf("init data: %v", config)
    if yaml.Unmarshal(content, &config) != nil {
        log.Fatalf("解析config.yaml出错: %v", err)
    }
    fmt.Printf("File config: %v", config)
}

文件:

server:
  port: 8080
  code: test

相关文章

网友评论

      本文标题:go使用yaml解析

      本文链接:https://www.haomeiwen.com/subject/mxdwmktx.html