【问题标题】:Unmarshal a particular structure as string in Golang (yaml)在 Golang (yaml) 中将特定结构解组为字符串
【发布时间】:2021-02-11 10:26:27
【问题描述】:

假设我有一个简单的 yaml,例如:-

info: []
return:
- foo.com:
    some_value:
        1.1.1.1: true
    some_value2:
        hey: hi

如何将foo.com 的值作为字符串获取?我真的不在乎里面是什么结构。

使用流行的库gopkg.in/yaml.v2,我尝试了这样的结构:-

type ExecuteJidResp struct {
    Info   []interface{}            `yaml:"info"`
    Return []map[string]interface{} `yaml:"return,omitempty"`
}

但是,它会出错:-

panic: yaml: line 4: found character that cannot start any token

完整代码:https://play.golang.org/p/RJtQ9V9qGEY

【问题讨论】:

标签: go yaml


【解决方案1】:

在你的情况下是错误的风险

panic: yaml: line 4: found character that cannot start any token [recovered]
    panic: yaml: line 4: found character that cannot start any token

yaml 格式中:

yourBody := `info: []
return:
- foo.com:
    some_value:
        1.1.1.1: true
    some_value2:
        hey: hi`

rightBody := `info: []
return:
- foo.com:
    some_value:
      1.1.1.1: true
    some_value2:
      hey: hi`

查看 fixed 结果 playground

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多