Golang中使用yaml.v2解析带有map[string]string的结构体失败。

huangapple go评论87阅读模式
英文:

Golang yaml.v2 struct with a map[string]string unmarshalling fails

问题

我正在使用gopkg.in/yaml.v2包,并尝试解析一个像这样的yaml文件:

Sizes:
  A: small
  B: small
  C: medium

我创建了一个类似这样的go结构体:

type sizesByType struct {
    Map map[string]string `yaml:"Sizes"`
}

但是使用yaml.v2进行解析时,得到的是一个空的map。

我做错了什么?

英文:

Im am using the gopkg.in/yaml.v2 package and I'm trying to unmarshal a yaml file like this:

Sizes: 
  A: small
  B: small
  C: medium

I created a go struct like this:

type sizesByType struct {
    Map map[string]string `yaml: "Sizes"`
}

but unmarshaling it with yaml.v2 gives me an empty map.

What am I doing wrong?

答案1

得分: 3

移除你的结构标签中的空格:

type sizesByType struct {
    Map map[string]string `yaml:"Sizes"`
}
英文:

Remove the space in your struct tag:

type sizesByType struct {
    Map map[string]string `yaml:"Sizes"`
}

huangapple
  • 本文由 发表于 2017年3月18日 20:03:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/42874174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定