从 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
键的映射,其中包含 document
s 数组:
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文解释了GO的软件包导入机制:命名imports(例如导入“ fmt”)和空白导入(例如导入_ fmt; fmt;)。 命名导入使包装内容可访问,而空白导入仅执行t

本文解释了Beego的NewFlash()函数,用于Web应用程序中的页间数据传输。 它专注于使用newflash()在控制器之间显示临时消息(成功,错误,警告),并利用会话机制。 Lima

本文详细介绍了MySQL查询结果的有效转换为GO结构切片。 它强调使用数据库/SQL的扫描方法来最佳性能,避免手动解析。 使用DB标签和Robus的结构现场映射的最佳实践

本文演示了创建模拟和存根进行单元测试。 它强调使用接口,提供模拟实现的示例,并讨论最佳实践,例如保持模拟集中并使用断言库。 文章

本文探讨了GO的仿制药自定义类型约束。 它详细介绍了界面如何定义通用功能的最低类型要求,从而改善了类型的安全性和代码可重复使用性。 本文还讨论了局限性和最佳实践

本文详细介绍了在GO中详细介绍有效的文件,将OS.WriteFile(适用于小文件)与OS.openfile和缓冲写入(最佳大型文件)进行比较。 它强调了使用延迟并检查特定错误的可靠错误处理。

本文使用跟踪工具探讨了GO应用程序执行流。 它讨论了手册和自动仪器技术,比较诸如Jaeger,Zipkin和Opentelemetry之类的工具,并突出显示有效的数据可视化
