1 回答

TA贡献1804条经验 获得超8个赞
通过调试,我发现了多个问题。首先,yaml似乎并不关心字段名称。您必须使用注释字段
`yaml:"NAME"`
其次,在 YAML 文件中,Include两者Exclude都只包含一个字符串列表,而不是类似于地图的东西。所以你的结构变成:
type Paths struct {
Include []string `yaml:"Include"`
Exclude []string `yaml:"Exclude"`
}
它有效。完整代码:
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
var str string = `
'Include':
- 'string1'
- 'string2'
'Exclude':
- 'string3'
- 'string4'
`
type Paths struct {
Include []string `yaml:"Include"`
Exclude []string `yaml:"Exclude"`
}
func main() {
paths := Paths{}
err := yaml.Unmarshal([]byte(str), &paths)
fmt.Printf("%v\n", err)
fmt.Printf("%+v\n", paths)
}
- 1 回答
- 0 关注
- 189 浏览
添加回答
举报