解析地图Yaml错误

I've the following program in which I need to parse yaml with the following structure

https://codebeautify.org/yaml-validator/cbabd352 this is valid yaml and I use byte to make it more simple maybe the indentation was changed during the copy paste to the question but you can see in the link that the yaml is valid

The yaml have an api_version and runners, for each runner (key which is name) I've a list of commands and I need to print those commands for function1 and function4 ,what am I doing wrong here ?

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

var runContent = []byte(`
api_ver: 1
runners:
 - name: function1
   type:
    - command: spawn child process
    - command: build
    - command: gulp
 - name: function2
   type:
    - command: run function 1
 - name: function3
   type:
    - command: ruby build
 - name: function4
   type:
    - command: go build
`)

type Result struct {
    Version string    `yaml:"api_ver"`
    Runners []Runners `yaml:"runners"`
}

type Runners struct {
    Name string    `yaml:"name"`
    Type []Command `yaml:"type"`
}

type Command struct {
    Command string `yaml:"command"`
}

func main() {

    var runners []Result
    err := yaml.Unmarshal(runContent, &runners)
    if err != nil {
        log.Fatalf("Error : %v", err)
    }
    fmt.Printf("%+v", runners[0])
}

The error which I got cannot unmarshal !!map into []main.Result

I cannot change the yaml and it should be exactly like this

https://codebeautify.org/yaml-validator/cbabd352

This is the code https://play.golang.org/p/zidjOA6-gc7

The yaml that you have provided contains error in token. Validate the yaml used in your code here https://codebeautify.org/yaml-validator/cbaabb32

After that Create a variable of struct type result not an array. Because the yaml that you are using is creating a struct with Runners array and api_version.

This

var runners []Result

should be

var runners Result

Since because the struct is not a slice. To fetch the list of command for a name of function used in yaml. You need to loop over the runners array to find the name of function and get the value of commands for that function. Below is the working code:

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

var runContent = []byte(`
api_ver: 1
runners:
  - name: function1
    type:
    - command: spawn child process
    - command: build
    - command: gulp
  - name: function2
    type:
    - command: run function 1
    - name: function3
    type:
    - command: ruby build
  - name: function4
    type:
  - command: go build
`)

type Result struct {
    Version string    `yaml:"api_ver"`
    Runners []Runners `yaml:"runners"`
}

type Runners struct {
    Name string    `yaml:"name"`
    Type []Command `yaml:"type"`
}

type Command struct {
    Command string `yaml:"command"`
}

func main() {

    var runners Result
    // parse mta yaml
    err := yaml.Unmarshal(runContent, &runners)
    if err != nil {
        log.Fatalf("Error : %v", err)
    }
    commandList := getCommandList("function1", runners.Runners)
    fmt.Printf("%+v", commandList)
}

func getCommandList(name string, runners []Runners) []Command {
    var commandList []Command
    for _, value := range runners {
        if value.Name == name {
            commandList = value.Type
        }
    }
    return commandList
}

Playground example