亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在父子順序中構造yaml文件?

如何在父子順序中構造yaml文件?

Go
慕斯709654 2023-06-19 17:08:51
我想通過 yaml 文件使用 golang 形成一個結構,但我發現很難弄清楚如何去做。api:  local:    host: localhost    port: 8085  develop:    host:    port:  production:    host:    port:rest-api:  local:    host: localhost    port: 8085  develop:    host:    port:  production:    host:    port:這是我的 yaml 文件中的格式預期的代碼是我想在本地、開發和生產格式中創建一個動態 api url,如 api:local = host+port,與開發和生產相同,以便輕松地動態配置和設置感謝您在 golang struct 方面的幫助,也感謝您的幫助。
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

粘貼您的 yaml 會產生以下結果:

type AutoGenerated struct {

? ? API struct {

? ? ? ? Local struct {

? ? ? ? ? ? Host string `yaml:"host"`

? ? ? ? ? ? Port int? ? `yaml:"port"`

? ? ? ? } `yaml:"local"`

? ? ? ? Develop struct {

? ? ? ? ? ? Host interface{} `yaml:"host"`

? ? ? ? ? ? Port interface{} `yaml:"port"`

? ? ? ? } `yaml:"develop"`

? ? ? ? Production struct {

? ? ? ? ? ? Host interface{} `yaml:"host"`

? ? ? ? ? ? Port interface{} `yaml:"port"`

? ? ? ? } `yaml:"production"`

? ? } `yaml:"api"`

? ? RestAPI struct {

? ? ? ? Local struct {

? ? ? ? ? ? Host string `yaml:"host"`

? ? ? ? ? ? Port int? ? `yaml:"port"`

? ? ? ? } `yaml:"local"`

? ? ? ? Develop struct {

? ? ? ? ? ? Host interface{} `yaml:"host"`

? ? ? ? ? ? Port interface{} `yaml:"port"`

? ? ? ? } `yaml:"develop"`

? ? ? ? Production struct {

? ? ? ? ? ? Host interface{} `yaml:"host"`

? ? ? ? ? ? Port interface{} `yaml:"port"`

? ? ? ? } `yaml:"production"`

? ? } `yaml:"rest-api"`

}

有明顯的子類型重復項。所以可以修剪。


第一關:


type Address struct {

? ? Host string `yaml:"host"`

? ? Port int? ? `yaml:"port"`

}


type MyConfig struct {

? ? API struct {

? ? ? ? Local? ? ? Address `yaml:"local"`

? ? ? ? Develop? ? Address `yaml:"develop"`

? ? ? ? Production Address `yaml:"production"`

? ? } `yaml:"api"`

? ? RestAPI struct {

? ? ? ? Local? ? ? Address `yaml:"local"`

? ? ? ? Develop? ? Address `yaml:"develop"`

? ? ? ? Production Address `yaml:"production"`

? ? } `yaml:"rest-api"`

}

第二次(也是最后一次)通過:


type Address struct {

? ? Host string `yaml:"host"`

? ? Port int? ? `yaml:"port"`

}


type Deployment struct {

? ? Local? ? ? Address `yaml:"local"`

? ? Develop? ? Address `yaml:"develop"`

? ? Production Address `yaml:"production"`

}


type MyConfig struct {

? ? API? ? ?Deployment `yaml:"api"`

? ? RestAPI Deployment `yaml:"rest-api"`

}


查看完整回答
反對 回復 2023-06-19
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號