1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個贊
如果您只關(guān)心幾個特定的鍵,只需創(chuàng)建一個數(shù)據(jù)結(jié)構(gòu)來公開這些值:
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)
}
}
針對您的示例數(shù)據(jù)運(yùn)行上述代碼會產(chǎn)生:
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 關(guān)注
- 117 瀏覽
添加回答
舉報(bào)