Reading array from yaml file in GoLang

WBOY
Release: 2024-02-08 23:45:10
forward
1227 people have browsed it

从 GoLang 中的 yaml 文件读取数组

php editor Banana will introduce you to how to read arrays from yaml files in GoLang in this article. GoLang is a powerful programming language, and yaml files are a file format used to store structured data. By reading the array in the yaml file, we can easily obtain the data in it and perform subsequent processing. This article will explain in detail the steps to read a yaml file and provide sample code to help you understand better. Whether you are a beginner or an experienced developer, this article will provide you with practical tips and knowledge that you can easily apply to your projects. Let's get started right away!

Question content

I am trying to read a yaml file containing an array of objects

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{}
}
Copy after login

yaml file

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"}
Copy after login
Copy after login

mistake

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

I'm not sure if the problem is the yaml file syntax since it's similar to json, but the cross reference documentation looks correct.

Any suggestions would be greatly appreciated...

Workaround

You have defined the outermost container as a map with the document key , which contains an array of documents:

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

But this is not the structure of the input data, it looks like this:

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"}
Copy after login
Copy after login

The outer element is a map containing the documentationinfo key (the value of which is a map with the document key). You need to redefine your type like this (I've converted it to package main so I can run it locally for testing):

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)
}
Copy after login

Running the above code will produce:

65bcd85880 fee

The above is the detailed content of Reading array from yaml file in GoLang. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!