3 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊
首先,您需要將 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)
你將會(huì)有
map[item1:{One:some Two:some string} item2:{One:some Two:some string}]

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
您的映射存在多個(gè)問題:
Item不導(dǎo)出結(jié)構(gòu)成員。您必須導(dǎo)出它們:
type Item struct {
One string `yaml:"one"`
Two string `yaml:"two"`
}
Items是Items的映射數(shù)組
type conf struct {
Items []map[string]Item `yaml:"items"`
}

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
我的場(chǎng)合是,我將 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轉(zhuǎn)換為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"`
...
}
它報(bào)告一個(gè)錯(cuò)誤:yaml: unmarshal errors: line 5: cannot unmarshal !!seq into map[string]*config.Dependency。
我搜索了相關(guān)問題和yaml教程,我認(rèn)為我的yaml文件是可以的。我真的很困惑。
- 3 回答
- 0 關(guān)注
- 1291 瀏覽
添加回答
舉報(bào)