【发布时间】:2021-06-04 21:42:14
【问题描述】:
我正在寻找解组简单的 yaml,但有些地方不对劲。已经花了足够的时间。有什么帮助吗?
package main
import (
"fmt"
yaml "gopkg.in/yaml.v2"
)
func main() {
raw := `
targets:
- from: "http://localhost:8080/test1"
timeout: "10s"
- from: "http://localhost:8080/test2"
timeout: "30s"
`
type Target struct {
from string `yaml:"from"`
timeout string `yaml:"timeout"`
}
type config struct {
Targets []Target `yaml:"targets"`
}
cfg := config{}
err := yaml.Unmarshal([]byte(raw), &cfg)
if err != nil {
fmt.Println(err)
}
fmt.Println("Config", cfg)
}
我低于空 o/p
Config {[{ } { }]}
【问题讨论】:
-
顺便说一句,如果 go 编译器在这种情况下给出一些警告会更容易。
-
我认为您之所以投反对票,是因为这被认为是一个基本问题。我很感激你有一个游乐场链接并展示了你尝试过的东西。在我看来,这是一个合理的问题。
-
这个“{YML,JSON unmarshaling doesn't work!”-在 SO 上每周出现 2 到 10 次问题,答案总是一样的:导出你的字段!也不是这里的文档不清楚,或者官方文档中的示例会使用未导出的字段。