如何在Go中解组YAML

时间:2015-03-12 10:18:00

标签: go yaml

我很难在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
    }
}

1 个答案:

答案 0 :(得分:6)

将类型声明更改为:

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