如何在 Go 中解组 YAML

How to unmarshal YAML in Go

我很难用 Go 解组这段 YAML。我得到的错误是 cannot unmarshal !!seq into map[string][]map[string][]string。我试过各种地图都没有成功 (map[string]string ; []map[string]string 等等)

import (
    "gopkg.in/yaml.v1"
    "io/ioutil"
)

type AppYAML struct {
    Runtime       string                           `yaml:"runtime,omitempty"`
    Handlers      map[string][]map[string][]string `yaml:"handlers,omitempty"`
    Env_Variables map[string]string                `yaml:"env_variables,omitempty"`
}

func main() {
    s := `
runtime: go
handlers:
- url: /.*
  runtime: _go_app
  secure: always
env_variables:
  something: 'test'
`

    var a AppYAML
    if err = yaml.Unmarshal([]byte(s), &a); err != nil {
        log.Error(err)
        return
    }
}

将类型声明更改为:

type AppYAML struct {
    Runtime       string              `yaml:"runtime,omitempty"`
    Handlers      []map[string]string `yaml:"handlers,omitempty"`
    Env_Variables map[string]string   `yaml:"env_variables,omitempty"`
}