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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用地圖將 YAML 解組到結(jié)構(gòu)中

使用地圖將 YAML 解組到結(jié)構(gòu)中

Go
有只小跳蛙 2022-03-03 19:53:19
我正在嘗試將 YAML 文件解組為包含兩個映射的結(jié)構(gòu)(使用go-yaml)。YAML 文件:'Include':    - 'string1'    - 'string2''Exclude':    - 'string3'    - 'string4'結(jié)構(gòu):type Paths struct {    Include map[string]struct{}    Exclude map[string]struct{}}嘗試解組的函數(shù)的簡化版本(即刪除錯誤處理等):import "gopkg.in/yaml.v2"func getYamlPaths(filename string) (Paths, error) {    loadedPaths := Paths{        Include: make(map[string]struct{}),        Exclude: make(map[string]struct{}),    }    filenameabs, _ := filepath.Abs(filename)    yamlFile, err := ioutil.ReadFile(filenameabs)    err = yaml.Unmarshal(yamlFile, &loadedPaths)    return loadedPaths, nil}正在從文件中讀取數(shù)據(jù),但解組函數(shù)沒有將任何內(nèi)容放入結(jié)構(gòu)中,并且沒有返回任何錯誤。我懷疑 unmarshal-function 無法將 YAML 集合轉(zhuǎn)換為map[string]struct{},但如前所述,它不會產(chǎn)生任何錯誤,而且我環(huán)顧四周尋找類似的問題,但似乎找不到任何錯誤。任何線索或見解將不勝感激!
查看完整描述

1 回答

?
胡說叔叔

TA貢獻1804條經(jīng)驗 獲得超8個贊

通過調(diào)試,我發(fā)現(xiàn)了多個問題。首先,yaml似乎并不關(guān)心字段名稱。您必須使用注釋字段


`yaml:"NAME"`

其次,在 YAML 文件中,Include兩者Exclude都只包含一個字符串列表,而不是類似于地圖的東西。所以你的結(jié)構(gòu)變成:


type Paths struct {

    Include []string `yaml:"Include"`

    Exclude []string `yaml:"Exclude"`

}

它有效。完整代碼:


package main


import (

    "fmt"

    "gopkg.in/yaml.v2"

)


var str string = `

'Include':

    - 'string1'

    - 'string2'


'Exclude':

    - 'string3'

    - 'string4'

`


type Paths struct {

    Include []string `yaml:"Include"`

    Exclude []string `yaml:"Exclude"`

}


func main() {

    paths := Paths{}


    err := yaml.Unmarshal([]byte(str), &paths)


    fmt.Printf("%v\n", err)

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

}


查看完整回答
反對 回復(fù) 2022-03-03
  • 1 回答
  • 0 關(guān)注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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