【问题标题】:YAML Unmarshal map[string]structYAML Unmarshal map[string]struct
【发布时间】:2020-09-08 14:41:45
【问题描述】:

我绝对没有得到这个。这是一个给定的 yaml 文件

items:
  - item1:
      one: "some"
      two: "some string"
  - item2:
      one: "some"
      two: "some string"

还有一个配置:

type Item struct {
    one string
    two string
}
type conf struct {
    Items map[string]Item
}

func (c *conf) getConfig(filename string) *conf {

    yamlFile, err := ioutil.ReadFile(filename)
    if err != nil {
        log.Printf("yamlFile.Get err   #%v ", err)
    }
    err = yaml.Unmarshal(yamlFile, &c)
    if err != nil {
        log.Fatalf("Unmarshal: %v", err)
    }

    //c.Items = make(map[string]Items)
    return c
}

我正在使用gopkg.in/yaml.v2

出现此错误:

Unmarshal: yaml: unmarshal errors:
  line 6: cannot unmarshal !!seq into map[string]application.Item

请帮助我理解我在这里做错了什么。我已经到处搜索了。 提前致谢。

【问题讨论】:

    标签: go yaml


    【解决方案1】:

    您的映射存在多个问题:

    • Item 结构成员未导出。您必须导出它们:
    type Item struct {
        One string `yaml:"one"`
        Two string `yaml:"two"`
    }
    
    • ItemsItems 的映射数组
    type conf struct {
        Items []map[string]Item `yaml:"items"`
    }
    

    【讨论】:

    • 非常感谢!我应该更加注意结构和导出的项目!刚开始学习 GO
    【解决方案2】:

    首先,您需要将 YAML 更改为

      items:
          item1:
            one: "some"
            two: "some string"
          item2:
            one: "some"
            two: "some string"
    

    然后,在你的 go 代码中

    type Config struct {
        Items map[string]Item
    }
    
    type Item struct {
        One string
        Two string
    }
    

    然后用

    fmt.Printf("%+v\n", c.Items)
    

    你会有

    map[item1:{One:some Two:some string} item2:{One:some Two:some string}]
    

    【讨论】:

      【解决方案3】:

      我的场合是,我将yaml定义如下:

      idl:
        protobuf:
          executable: protoc
          version_min: v3.6.0
          version_cmd: "protoc --version | awk '{ print $2 }'"
          install_cmd: ""
          fallback: "please install protoc first, see: https://github.com/protocolbuffers/protobuf"
        flatbuffers:
          executable: flatc
          version_min: 2.0.0
          version_cmd: "flatc --version | awk '{ print $3 }'"
          install_cmd: ""
          fallback: "please install flatc first, see: https://google.github.io/flatbuffers/flatbuffers_guide_building.html"
      

      我想把yaml转换成Config类型,定义为:

      type Dependency struct {
          Executable string `yaml:"executable"`  
          VersionMin string `yaml:"version_min"` 
          VersionCmd string `yaml:"version_cmd"` 
          InstallCmd string `yaml:"install_cmd"` 
          Fallback   string `yaml:"fallback"`    
      }
      
      type Config struct {
          IDL       map[string]*Dependency          `yaml:"idl"`
          ...
      }
      

      报错:yaml: unmarshal errors: line 5: cannot unmarshal !!seq into map[string]*config.Dependency

      我搜索了相关问题和yaml教程,我认为我的yaml文件是可以的。我真的很困惑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-09
        • 1970-01-01
        • 1970-01-01
        • 2020-09-23
        • 2014-08-04
        • 2019-03-30
        • 2019-03-22
        • 1970-01-01
        相关资源
        最近更新 更多