1 回答

TA貢獻1887條經驗 獲得超5個贊
hello 對象被覆蓋,因為您每次都在創(chuàng)建一個新的 hello 對象,如果您不想覆蓋只是更新對象。
type Hello struct {
Title Locale `json:"title"`
Content Locale `json:"content"
}
type Locale struct {
EN_US string `json:"en-US"`
ZH_HK string `json:"zh-HK"`
ZH_CN string `json:"zh-CN"`
}
someMap := map[int]Hello{}
for _, recordTranslations := range recordsTranslations {
if _, has := someMap[recordTranslations.SchoolGroupID]; !has {
someMap[recordTranslations.SchoolGroupID] = Hello{}
}
if recordTranslations.Locale == "en-US" {
someMap[recordTranslations.SchoolGroupID].Title.EN_US = recordTranslations.Title
someMap[recordTranslations.SchoolGroupID].Content.EN_US = recordTranslations.Content
} else if recordTranslations.Locale == "zh-HK" {
someMap[recordTranslations.SchoolGroupID].Title.ZH_HK = recordTranslations.Title
someMap[recordTranslations.SchoolGroupID].Content.ZH_HK = recordTranslations.Content
} else if recordTranslations.Locale == "zh-CN" {
someMap[recordTranslations.SchoolGroupID].Title.ZH_CN = recordTranslations.Title
someMap[recordTranslations.SchoolGroupID].Content.ZH_CN = recordTranslations.Content
}
}
- 1 回答
- 0 關注
- 112 瀏覽
添加回答
舉報