1 回答
TA贡献1887条经验 获得超5个赞
hello 对象被覆盖,因为您每次都在创建一个新的 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 关注
- 94 浏览
添加回答
举报