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"`
}
- 1 回答
- 0 關注
- 149 瀏覽
添加回答
舉報