Yaml 输入:Tasks: - task: name: 123 retry: 5 next: 123 - task: name: 123 retry: 5 next: 123代码:package mainimport ( "fmt" "io/ioutil" "os" "gopkg.in/yaml.v2")type Tasks []struct { Task Task `yaml:"task"`}type Task struct { Name string `yaml:"name"` Retry int `yaml:"retry"` Next string `yaml:"next"`}func main() { var w Tasks wfyaml, _ := os.Open(".yaml") byteValue, _ := ioutil.ReadAll(wfyaml) yaml.Unmarshal(byteValue, &w) fmt.Printf("%+v\n", w)}输出:[]我无法弄清楚问题的原因是什么。我已经检查了我能做的。
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
Tasks您错过了结构(游乐场)中的顶层():
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
const yamlStr = `Tasks:
- task:
name: 123
retry: 5
next: 123
- task:
name: 123
retry: 5
next: 123`
type File struct {
Tasks Tasks `yaml:"Tasks"`
}
type Tasks []struct {
Task Task `yaml:"task"`
}
type Task struct {
Name string `yaml:"name"`
Retry int `yaml:"retry"`
Next string `yaml:"next"`
}
func main() {
var w File
yaml.Unmarshal([]byte(yamlStr), &w)
fmt.Printf("%+v\n", w)
}
- 1 回答
- 0 关注
- 81 浏览
添加回答
举报
0/150
提交
取消