Is it possible to do this in golang?

Viewed 60

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 Answers

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"`
}
Related