亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

Golang YAML 閱讀與地圖地圖

Golang YAML 閱讀與地圖地圖

Go
慕無忌1623718 2021-08-23 15:43:22
這是我的 YAML 文件。description: fruits are deliciousfruits:  apple:    - red    - sweet  lemon:    - yellow    - sour我可以用這個gopkg.in/yaml.v1包讀取更扁平的版本,但是當它看起來像地圖時,我一直試圖弄清楚如何讀取這個 YAML 文件。package mainimport (  "fmt"  "gopkg.in/yaml.v1"  "io/ioutil"  "path/filepath")type Config struct {  Description string  Fruits []Fruit}type Fruit struct {  Name string  Properties []string}func main() {  filename, _ := filepath.Abs("./file.yml")  yamlFile, err := ioutil.ReadFile(filename)  if err != nil {    panic(err)  }  var config Config  err = yaml.Unmarshal(yamlFile, &config)  if err != nil {    panic(err)  }  fmt.Printf("Value: %#v\n", config.Description)  fmt.Printf("Value: %#v\n", config.Fruits)}它無法取出嵌套的 Fruits。回來好像是空的。 Value: []main.Fruit(nil).
查看完整描述

1 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

使用字符串切片圖來表示水果屬性:


type Config struct {

  Description string

  Fruits map[string][]string

}

打印未編組的配置


fmt.Printf("%#v\n", config)

產生以下輸出(不包括我為可讀性添加的空格):


main.Config{Description:"fruits are delicious", 

     Fruits:map[string][]string{

          "lemon":[]string{"yellow", "sour"}, 

          "apple":[]string{"red", "sweet"}}}


查看完整回答
反對 回復 2021-08-23
  • 1 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號