第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

YAML Unmarshal map[string]struct

YAML Unmarshal map[string]struct

Go
POPMUISE 2022-06-06 14:58:08
我絕對(duì)沒有得到這個(gè)。這是給定的 yaml 文件items:  - item1:      one: "some"      two: "some string"  - item2:      one: "some"      two: "some string"和一個(gè)配置: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出現(xiàn)此錯(cuò)誤:Unmarshal: yaml: unmarshal errors:  line 6: cannot unmarshal !!seq into map[string]application.Item請(qǐng)幫助我理解我在這里做錯(cuò)了什么。我已經(jīng)到處搜索了。提前致謝。
查看完整描述

3 回答

?
哈士奇WWW

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}]


查看完整回答
反對(duì) 回復(fù) 2022-06-06
?
qq_花開花謝_0

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"`

}


查看完整回答
反對(duì) 回復(fù) 2022-06-06
?
慕蓋茨4494581

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文件是可以的。我真的很困惑。


查看完整回答
反對(duì) 回復(fù) 2022-06-06
  • 3 回答
  • 0 關(guān)注
  • 1291 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)