前往:解析变数YAML

I want to unmarshall map, which can contain different values, how can I do it in Go with yaml.v2?

Currently, I have the following node description:

package executors

type OptionMap map[string][]string

type Step struct {
    Exec string
    Pwd  string
    Opts OptionMap
}

Sample YAML is the following:

steps:
   - exec: maven
     pwd: /code
     opts:
       goals:
         - clean
         - install
       mvn_home: /maven

Obviously, node will be unmarshaled if only goals, but not mvn_home will present there. Is there any way to write the single description for such node?

If no, is there any way to read some part of YAML (under some key) as a plain string or, better, such sort of map as map[string]OBJECT and unmarshal that OBJECT separately?

Try using map[string]interface{} to unmarshal. With interface{} you can unmarshal it later when required.