英文:
Is it possible to do this in golang?
问题
我对golang中的.yaml
文件有一个疑问,假设我有一个包含以下内容的.yaml
文件:
print:
1
print:
2
print:
3
有没有办法获取yaml文件中的所有print
?在golang中如何表示这种结构?例如,如果我在一个.yaml
文件中有以下内容:
print:
1
在golang中,我可以这样表示它:
type Print struct {
Print int `yaml:"print"`
}
如果无法这样做,还有其他类似的方法吗?提前谢谢。
英文:
I have a doubt about the .yaml
files in golang, suppose I have a .yaml
file with the following content:
print:
1
print:
2
print:
3
Is there a way to get all the print
in the yaml file? How would I represent that structure in golang? Because for example if I have this in a .yaml
file:
print:
1
In golang I can represent it like this:
type Print struct {
Print int `yaml:"print"`
}
And if that cannot be done, what other way would there be to do something similar? Thanks in advance.
答案1
得分: 6
你的YAML不合法。在映射中,不能有相同的键出现多次。根据规范的3.2.1.1节:
映射节点的内容是一组无序的键/值节点对,限制条件是每个键都是唯一的。
相反,你可以使用映射到序列的方式。
print: [1,2,3]
并将其存储为[]int
类型。
type Print struct {
Print []int `yaml:"print"`
}
英文:
Your YAML is not legal. You can't have the same key multiples times in a mapping. From 3.2.1.1 of the spec...
> The content of a mapping node is an unordered set of key/value node pairs, with the restriction that each of the keys is unique.
Instead, use a mapping to a sequence.
print: [1,2,3]
And store it as an []int
.
type Print struct {
Print []int `yaml:"print"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论