Having a problem parsing this sort of yaml file. Using "yaml.v2"
info: "abc"
data:
source: http://intra
destination: /tmp
run:
- id: "A1"
exe: "run.a1"
output: "output.A1"
- id: "A2"
exe: "run.a2"
output: "output.A2"
I would like to get all the values of the YAML file so I have a basic struct like this
type Config struct {
Info string
Data struct {
Source string `yaml:"source"`
Destination string `yaml:"destination"`
}
}
This works
But, I am not sure how to setup the struct for "run". The extra layer is confusing me.
type Run struct {
...
}
the OP's example of YAML is invalid. When value of run
is list of dictionary it should be something like this:
info: "abc"
data:
source: http://intra
destination: /tmp
run:
- id: "A1"
exe: "run.a1"
output: "output.A1"
- id: "A2"
exe: "run.a2"
output: "output.A2"
And here's the corresponding data struture, and example for decoding YAML into golang's structure.
package main
import (
"fmt"
"io/ioutil"
"os"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
Info string
Data struct {
Source string
Destination string
}
Run []struct {
Id string
Exe string
Output string
}
}
func main() {
var conf Config
reader, _ := os.Open("example.yaml")
buf, _ := ioutil.ReadAll(reader)
yaml.Unmarshal(buf, &conf)
fmt.Printf("%+v
", conf)
}
running this will output (added some indent for readability):
{Info:abc
Data:{Source:http://intra Destination:/tmp}
Run:[{Id:A1 Exe:run.a1 Output:output.A1}
{Id:A2 Exe:run.a2 Output:output.A2}]