我有 config.yml 檔案:
vcenter:
connection: https
hostname: vcenter.mydomain.lan
username: [email protected]
password: mypasspord
port: 443
EsxiExcludeDatastores:
- datastore1
- datastore2
EsxiExcludeDatastores2:
datastores:
- datastore1
- datastore2
我試圖用“gopkg.in/yaml.v2”決議它
我創建了結構:
type FileConfig struct {
Vcenter struct {
ConnectionType string `yaml:"connection"`
Hostname string `yaml:"hostname"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Port int `yaml:"port"`
} `yaml:"vcenter"`
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
}
EsxiExcludeDatastores2 struct {
Datastores []string `yaml:"datastores"`
} `yaml:"EsxiExcludeDatastores2"`
}
var AppConfig FileConfig
決議后,我列印結果:
fmt.Println("Num of EsxiExcludeDatastores.Datastores = ", len(AppConfig.EsxiExcludeDatastores.Datastores))
fmt.Println("Num of EsxiExcludeDatastores2.Datastores = ", len(AppConfig.EsxiExcludeDatastores2.Datastores))
Num of EsxiExcludeDatastores.Datastores = 0
Num of EsxiExcludeDatastores2.Datastores = 2
你能幫我嗎,我在 EsxiExcludeDatastores 結構中做錯了什么?如您所見,EsxiExcludeDatastores2 一切正常,但 EsxiExcludeDatastores 為空。
我試圖以不同的方式做到這一點,但沒有結果......
uj5u.com熱心網友回復:
您快到了。
要將 yaml 結構標記“提升”到“父”欄位,請添加:
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
} `yaml:",inline"` // <- this
https://play.golang.org/p/S7QxGaspTyN
從yaml.v2檔案中,使用該inline標志的結構標記執行以下操作:
行內欄位,該欄位必須是結構或映射,從而使其所有欄位或鍵都被處理,就好像它們是外部結構的一部分一樣。對于映射,鍵不能與其他結構體欄位的 yaml 鍵沖突。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358297.html
