go - 如何在 Go 中解码 YAML

标签 go yaml

我很难用 Go 解码这段 YAML。我得到的错误是 cannot unmarshal !!seq into map[string][]map[string][]string。我已经尝试了所有类型的 map 但都没有成功(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"`
}

关于go - 如何在 Go 中解码 YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29007036/

相关文章:

go - 在 Go 中发送多个请求时出现零星的 EOF 错误

azure - Terraform Plan 在 Windows 上为列表变量传递值

php - Doctrine2 oneToMany 关系 yaml

docker - 将 args 传递给容器时 Kubernetes pod/部署?

go - 在 Golang 中找出正确的结构来解析一个简单的 YAML 文件

unit-testing - 导出 Golang 包进行测试?

go - 动态初始化结构

go - 如何解码 map golang

Golang net.Listen IPv6

aws-lambda - 通过 cloudformation yaml 添加带有错误操作的 IoT 规则