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

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

無法將 yaml 文件解組到結構中

無法將 yaml 文件解組到結構中

Go
波斯汪 2023-07-17 14:49:23
我正在嘗試 UnmarshalS 到 DataCollectionFromYAML--- -   labels: cats, cute, funny  name: "funny cats"  url: "http://glorf.com/videos/asfds.com"-   labels: cats, ugly,funny  name: "more cats"  url: "http://glorf.com/videos/asdfds.com"-   labels: dogs, cute, funny  name: "lots of dogs"  url: "http://glorf.com/videos/asasddfds.com"-   name: "bird dance"  url: "http://glorf.com/videos/q34343.com"type DataFromYAML struct {    Labels string `yaml:"labels"`    Name   string `yaml:"name"`    URL    string `yaml:"url"`}type DataCollectionFromYAML struct {    data []VidedFromYAML}這是我的代碼的一部分,我正在使用 gopkg.in/yaml.v2 包yamlFile, err := ioutil.ReadAll(r)    if err != nil {        return err    }    var data models.DataFromYAML    err2 := yaml.Unmarshal(yamlFile, data)我收到錯誤消息:無法將 !!seq 解組到 models.DataCollectionFromYAML
查看完整描述

1 回答

?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

安裝了 包 mainmodels.DataFromYAML的 use 數組[]models.DataFromYAML


import (

    "fmt"


    "github.com/ghodss/yaml"

)



const data = `--- 

  labels: cats, cute, funny

  name: "funny cats"

  url: "http://glorf.com/videos/asfds.com"

  labels: cats, ugly,funny

  name: "more cats"

  url: "http://glorf.com/videos/asdfds.com"

  labels: dogs, cute, funny

  name: "lots of dogs"

  url: "http://glorf.com/videos/asasddfds.com"

  name: "bird dance"

  url: "http://glorf.com/videos/q34343.com"

`


type DataFromYAML struct {

    Labels string `yaml:"labels"`

    Name   string `yaml:"name"`

    URL    string `yaml:"url"`

}



func main() {

    var test []DataFromYAML

    err := yaml.Unmarshal([]byte(data), &test)

    if err != nil {

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

        return

    }


    fmt.Println(test)

}

輸出:


[{cats, cute, funny funny cats http://glorf.com/videos/asfds.com} {cats, ugly,funny more cats http://glorf.com/videos/asdfds.com} {dogs, cute, funny lots of dogs http://glorf.com/videos/asasddfds.com} { bird dance http://glorf.com/videos/q34343.com}]



查看完整回答
反對 回復 2023-07-17
  • 1 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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