以下是一个加载a.yaml文件中内容的Go代码:
package main
import (
"fmt"
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
)
type YamlItem struct {
Id string `yaml:"Id"`
FileRe string `yaml:"FileRe"`
SecondMethod string `yaml:"SecondMethod"`
SecondValue string `yaml:"SecondValue"`
Desc string `yaml:"Desc"`
Flag string `yaml:"Flag"`
ModifyAt string `yaml:"ModifyAt"`
}
func main() {
content, err := ioutil.ReadFile("a.yaml")
if err != nil {
log.Fatal(err)
}
var items []YamlItem
err = yaml.Unmarshal(content, &items)
if err != nil {
log.Fatal(err)
}
for _, item := range items {
fmt.Printf("Id: %s\nFileRe: %s\nSecondMethod: %s\nSecondValue: %s\nDesc: %s\nFlag: %s\nModifyAt: %s\n",
item.Id, item.FileRe, item.SecondMethod, item.SecondValue, item.Desc, item.Flag, item.ModifyAt)
}
}
输出结果将为:
Id: 102
FileRe: .*
SecondMethod: re_ver_collect
SecondValue: ([A-Za-z0-9]{32})
Desc: Tencent Secret Key
Flag: tencent-secret-key
ModifyAt:
Id: 102
FileRe: .*
SecondMethod: re_ver_collect
SecondValue: ([A-Za-z0-9]{32})
Desc: Tencent Secret Key
Flag: tencent-secret-key
ModifyAt: