1 回答
TA贡献1828条经验 获得超13个赞
如果您只关心几个特定的键,只需创建一个数据结构来公开这些值:
package main
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v3"
)
type (
Entry struct {
TestName string `yaml:"TEST_NAME"`
RunPath string `yaml:"RUN_PATH"`
}
)
func main() {
reportYAML := os.Args[1]
// userDictionary := os.Args[2]
yfile, err := ioutil.ReadFile(reportYAML)
if err != nil {
fmt.Printf("ERROR: Unable to open yaml file : %s\n", err)
}
data := make(map[string]Entry)
error := yaml.Unmarshal([]byte(yfile), &data)
if error != nil {
fmt.Printf("ERROR: Unable to read yaml file : %s\n", err)
}
for _, value := range data {
fmt.Printf("test_name: %s\n", value.TestName)
fmt.Printf("run_path: %s\n", value.RunPath)
}
}
针对您的示例数据运行上述代码会产生:
test_name: THIS/IS/WHAT/IS/DISPLAYS/ON/THE/HTML
run_path: example/testfiles/release
test_name: USED/FOR/THE/HTML
run_path: example/testfiles/foo
- 1 回答
- 0 关注
- 94 浏览
添加回答
举报