从 GoLang 中的 yaml 文件读取数组

2024年 2月 9日 38.8k 0

从 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("%+vn", docmap)
}

登录后复制

运行上面的代码会产生:

65bcd85880费用

以上就是从 GoLang 中的 yaml 文件读取数组的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论