php小编香蕉在本文中将为您介绍如何从GoLang中的yaml文件中读取数组。GoLang是一种强大的编程语言,yaml文件则是一种用于存储结构化数据的文件格式。通过读取yaml文件中的数组,我们可以轻松地获取其中的数据并进行后续处理。本文将会详细解释读取yaml文件的步骤,并提供示例代码以帮助您更好地理解。无论您是初学者还是有一定经验的开发者,本文都将为您提供实用的技巧和知识,让您能够轻松应用到您的项目中。让我们马上开始吧!
问题内容
我正在尝试读取包含对象数组的 yaml 文件
package configimport ( "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"}
登录后复制登录后复制
错误
立即学习“go语言免费学习笔记(深入)”;
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 mainimport ( "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", docmap)}
登录后复制
运行上面的代码会产生:
65bcd85880费用
以上就是从 GoLang 中的 yaml 文件读取数组的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2358501.html