从 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脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

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

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

Dreamweaver CS6
视觉化网页开发工具

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

热门话题

OpenSSL,作为广泛应用于安全通信的开源库,提供了加密算法、密钥和证书管理等功能。然而,其历史版本中存在一些已知安全漏洞,其中一些危害极大。本文将重点介绍Debian系统中OpenSSL的常见漏洞及应对措施。DebianOpenSSL已知漏洞:OpenSSL曾出现过多个严重漏洞,例如:心脏出血漏洞(CVE-2014-0160):该漏洞影响OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻击者可利用此漏洞未经授权读取服务器上的敏感信息,包括加密密钥等。

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

本文介绍在Debian系统下监控PostgreSQL数据库的多种方法和工具,助您全面掌握数据库性能监控。一、利用PostgreSQL内置监控视图PostgreSQL自身提供多个视图用于监控数据库活动:pg_stat_activity:实时展现数据库活动,包括连接、查询和事务等信息。pg_stat_replication:监控复制状态,尤其适用于流复制集群。pg_stat_database:提供数据库统计信息,例如数据库大小、事务提交/回滚次数等关键指标。二、借助日志分析工具pgBadg

后端学习路径:从前端转型到后端的探索之旅作为一名从前端开发转型的后端初学者,你已经有了nodejs的基础,...

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

Go语言中字符串打印的区别:使用Println与string()函数的效果差异在Go...

在BeegoORM框架下,如何指定模型关联的数据库?许多Beego项目需要同时操作多个数据库。当使用Beego...
