Let's say I have the following yaml file:
key1:
- "Value1"
- "Value2"
key2:
- "Value3"
- "Value4"
And I want to unmarshal that in Go. I can let Go decide what the scheme should look like and print it out.
m := make(map[interface{}]interface{})
err := yaml.Unmarshal(yaml_file, &m)
fmt.Prinf(m)
This will print something like this:
map[key1:[Value1 Value2] key2:[Value3 Value4]]
But maps are unordered. The order is very important for me tho and I also don't want to loose the keys.
I can't just create a struct and try to unmarshal the yaml file to a slice of that struct...
type Element struct {
Key string
Value interface{}
}
t := []Element{}
err := yaml.Unmarshal(yaml_file, &t)
if err != nil {
log.Fatalf("error: %v", err)
}
Will print error: yaml: unmarshal errors: line 2: cannot unmarshal !!map into []Element
There is no field tag to tell the yaml parser to fill the Key property with the yaml key.
Is there a different way than writing my own decoder?