1 回答
TA贡献1804条经验 获得超7个赞
安装了 包 mainmodels.DataFromYAML的 use 数组[]models.DataFromYAML
import (
"fmt"
"github.com/ghodss/yaml"
)
const data = `---
-
labels: cats, cute, funny
name: "funny cats"
url: "http://glorf.com/videos/asfds.com"
-
labels: cats, ugly,funny
name: "more cats"
url: "http://glorf.com/videos/asdfds.com"
-
labels: dogs, cute, funny
name: "lots of dogs"
url: "http://glorf.com/videos/asasddfds.com"
-
name: "bird dance"
url: "http://glorf.com/videos/q34343.com"
`
type DataFromYAML struct {
Labels string `yaml:"labels"`
Name string `yaml:"name"`
URL string `yaml:"url"`
}
func main() {
var test []DataFromYAML
err := yaml.Unmarshal([]byte(data), &test)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Println(test)
}
输出:
[{cats, cute, funny funny cats http://glorf.com/videos/asfds.com} {cats, ugly,funny more cats http://glorf.com/videos/asdfds.com} {dogs, cute, funny lots of dogs http://glorf.com/videos/asasddfds.com} { bird dance http://glorf.com/videos/q34343.com}]
- 1 回答
- 0 关注
- 108 浏览
添加回答
举报