首頁  >  文章  >  後端開發  >  從 GoLang 中的 yaml 檔案讀取數組

從 GoLang 中的 yaml 檔案讀取數組

WBOY
WBOY轉載
2024-02-08 23:45:101204瀏覽

从 GoLang 中的 yaml 文件读取数组

php小編香蕉在本文中將為您介紹如何從GoLang中的yaml檔案中讀取陣列。 GoLang是一種強大的程式語言,yaml檔案是一種用於儲存結構化資料的檔案格式。透過讀取yaml檔案中的數組,我們可以輕鬆地獲取其中的資料並進行後續處理。本文將會詳細解釋讀取yaml檔案的步驟,並提供範例程式碼以幫助您更好地理解。無論您是初學者還是有一定經驗的開發者,本文都將為您提供實用的技巧和知識,讓您輕鬆應用到您的專案中。讓我們馬上開始吧!

問題內容

我正在嘗試讀取包含物件陣列的 yaml 檔案

package config

import (
    "gopkg.in/yaml.v3"
    "log"
    "os"
    "path/filepath"
    "runtime"
)

type documentationinfo struct {
    docs []document `yaml:"document"`
}

type document struct {
    title    string `yaml:"title"`
    filename string `yaml:"filename"`
}

func (c *documentationinfo) init() map[string]document {
    _, b, _, _ := runtime.caller(0)
    basepath := filepath.dir(b)

    yamlfile, err := os.readfile(basepath + "/documentation.yaml")
    if err != nil {
        log.printf("yamlfile.get err   #%v ", err)
    }
    err = yaml.unmarshal(yamlfile, c.docs)
    if err != nil {
        log.fatalf("unmarshal: %v", err)
    }

    docinfo := make(map[string]document)

    for _, doc := range c.docs {
        docinfo[doc.filename] = doc
    }

    return docinfo
}
func newdocumentconfig() *documentationinfo {
    return &documentationinfo{}
}

yaml 檔案

documentationinfo:
  document:
    - {filename: "foo.md", title: "i am an foo title"}
    - {filename: "test.md", title: "i am an test title"}
    - {filename: "bar.md", title: "i am an bar title"}
    - {filename: "nice.md", title: "i am an nice title"}

錯誤

2023/06/27 13:44:44 Unmarshal: yaml: unmarshal errors:
  line 1: cannot unmarshal !!map into []config.Document

我不確定問題是否是 yaml 檔案語法,因為它與 json 類似,但交叉引用文件看起來是正確的。

如有任何建議,我們將不勝感激...

解決方法

您已將最外層容器定義為帶有document 鍵的映射,其中包含documents 陣列:

type documentationinfo struct {
  docs []document `yaml:"document"`
}

但這不是輸入資料的結構,它看起來像這樣:

documentationinfo:
  document:
    - {filename: "foo.md", title: "i am an foo title"}
    - {filename: "test.md", title: "i am an test title"}
    - {filename: "bar.md", title: "i am an bar title"}
    - {filename: "nice.md", title: "i am an nice title"}

外部元素是一個包含 documentationinfo 鍵的映射(該鍵的值是帶有 document 鍵的映射)。您需要像這樣重新定義您的類型(我已將其轉換為 package main,以便我可以在本地運行它進行測試):

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"
    "runtime"

    "gopkg.in/yaml.v3"
)

type documentationinfofile struct {
    documentationinfo documentationinfo `yaml:"documentationinfo"`
}

type documentationinfo struct {
    docs []document `yaml:"document"`
}

type document struct {
    title    string `yaml:"title"`
    filename string `yaml:"filename"`
}

func (docinfo *documentationinfofile) init() map[string]document {
    _, b, _, _ := runtime.caller(0)
    basepath := filepath.dir(b)

    yamlfile, err := os.readfile(basepath + "/documentation.yaml")
    if err != nil {
        log.printf("yamlfile.get err   #%v ", err)
    }
    err = yaml.unmarshal(yamlfile, docinfo)
    if err != nil {
        log.fatalf("unmarshal: %v", err)
    }

    docmap := make(map[string]document)
    for _, doc := range docinfo.documentationinfo.docs {
        docmap[doc.filename] = doc
    }

    return docmap

}
func newdocumentconfig() *documentationinfofile {
    return &documentationinfofile{}
}

func main() {
    d := newdocumentconfig()
    docmap := d.init()
    fmt.printf("%+v\n", docmap)
}

執行上面的程式碼會產生:

65bcd85880費用

以上是從 GoLang 中的 yaml 檔案讀取數組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除