更新对象的一部分

我希望有一张格式如下的地图:


mapName[someIntegerID].Title.EN_US


mapName[someIntegerID].Title.ZH_HK


mapName[someIntegerID].Title.ZH_CN


mapName[someIntegerID].Content.EN_US


mapName[someIntegerID].Content.ZH_HK


mapName[someIntegerID].Content.ZH_CN

其实上面说的有道理吗?无论如何,我尝试了下面的代码。api json 响应格式是完美的,但是,我注意到在 forloop 期间,zh_HK 将覆盖 en_US 的,以及 zh_CN 将覆盖 zh_HK 和 en_US,导致正确的 zh_CN 值但空 en_US 和 zh_HK 值。


专家可以给我一些建议,我该如何实现它?


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] = Hello{

            Title: Locale{

                EN_US: recordTranslations.Title,

            },

            Content: Locale{

                EN_US: recordTranslations.Content,

            },

        }

    } else if recordTranslations.Locale == "zh-HK" {

        someMap[recordTranslations.SchoolGroupID] = Hello{

            Title: Locale{

                ZH_HK: recordTranslations.Title,

            },

            Content: Locale{

                ZH_HK: recordTranslations.Content,

            },

        }


    } else if recordTranslations.Locale == "zh-CN" {

        someMap[recordTranslations.SchoolGroupID] = Hello{

            Title: Locale{

                ZH_CN: recordTranslations.Title,

            },

            Content: Locale{

                ZH_CN: recordTranslations.Content,

            },

        }

    }

}


猛跑小猪
浏览 95回答 1
1回答

慕工程0101907

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}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go