我正在嘗試決議具有以下結構的 YAML 檔案:
contacts:
teamone:
email:
to: '[email protected]'
我可以像這樣獲取電子郵件地址:
https://go.dev/play/p/gt_smMgdMCh
我的問題是,如何從 YAML 檔案中獲取所有電子郵件地址(或特定地址),如下所示:
contacts:
teamone:
email:
to: '[email protected]'
teamtwo:
email:
to: '[email protected]'
此外,人們可以隨時向此檔案添加新的團隊/電子郵件地址。
這樣做的目的是從 YAML 檔案中查找聯系方式。因此,有人可以運行該程式,為其提供一個團隊(例如“teamnine”),并且輸出將是與該條目關聯的電子郵件。
任何意見,將不勝感激。
uj5u.com熱心網友回復:
定義Team為單獨的結構,并使用映射:
type Team struct {
Email struct {
To string `yaml:"to"`
} `yaml:"email"`
}
type Contacts struct {
Contacts map[string]Team `yaml:"contacts"`
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/364753.html
標籤:走
