各位大佬,想要提取下面这份yaml, 应该如何定义数据结构来提取呢?
`- import: <xxx_root>/testlist.yaml`
`- test: xxx_invalid_test`
` description: >`
` xxx at a higher priv mode`
` iterations: 2`
` gen_test: xxx_random_test`
` gen_opts: >`
` +datar_cnt=1000`
` +num_of_block=2`
` +enable_invalid_level=1`
` func_test: main_invalid_test`
` sim_opts: >`
` +require_signature=1`
我用github.com/goccy/go-yaml, 做法如下:
`type YamlTestConfig struct {`
` TestName string `yaml:"test"``
` Description string `yaml:"description"``
` GenOptions []string `yaml:"gen_opts"``
` Iteration int `yaml:"iterations"``
` GenTestName string `yaml:"gen_test"``
` FuncTestName string `yaml:"rtl_test"``
` SimOptions []string `yaml:"sim_opts"``
`}`
`type ConfigInformation struct {`
` ImportFile string `yaml:"import"``
` TestConfigs []YamlTestConfig`
`}`
程序一直没有反应,麻烦大佬们指点一下,谢谢!
如3楼所说,tag应放在``内,另外,如果可能的话建议改下yaml格式:
```golang
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"os"
)
var yamlStr string = `
import: "/testlist.yaml"
tests:
- test: "xxx_invalid_test"
description: >
"xxx at a higher priv mode"
iterations: 13
`
type Config struct {
Import string `yaml:"import"`
Tests []struct {
Test string `yaml:"test"`
Description string `yaml:"description"`
Iterations int `yaml:"iterations"`
} `yaml:"tests"`
}
func main() {
var c Config
err := yaml.Unmarshal([]byte(yamlStr), &c)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
fmt.Println(c.Tests[0].Description)
}
```
> "xxx at a higher priv mode"
#4
更多评论
![image.png](https://static.studygolang.com/201218/d716aad641aa70db2548e23441014b69.png)
就是帖子上面的内容
#2