3 回答
TA贡献1799条经验 获得超6个赞
首先,您需要将 YAML 更改为
items:
item1:
one: "some"
two: "some string"
item2:
one: "some"
two: "some string"
然后,在你的代码中
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}]
TA贡献1835条经验 获得超7个赞
您的映射存在多个问题:
Item不导出结构成员。您必须导出它们:
type Item struct {
One string `yaml:"one"`
Two string `yaml:"two"`
}
Items是Items的映射数组
type conf struct {
Items []map[string]Item `yaml:"items"`
}
TA贡献1850条经验 获得超11个赞
我的场合是,我将 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文件是可以的。我真的很困惑。
- 3 回答
- 0 关注
- 1092 浏览
添加回答
举报