dongwen7187 2016-05-06 16:00
浏览 49
已采纳

循环添加条目到地图

I am trying to implement in my application a way to separate users per country and then, send them to different actions.

This is what I have right now:

var manyUsers zendesk.ManyUsers
users := make(map[string]zendesk.ManyUsers)

for {
    ...
    manyUsers.AppendUsers(mysqlRequest(country, id))
    users[country] = manyUsers

    fmt.Printf("[%s]: %#v

", country, users[country])
}

But I figure out that after when I add a 3 different countries, it will generate 3 diferent keys, but the last one will have the 3 users from the other countries.

This is my AppendUsers function

func (users *ManyUsers) AppendUsers(user User) []User {
    users.Users = append(users.Users, user)

    return users.Users
}
  • 写回答

3条回答 默认 最新

  • dongou5100 2016-05-06 17:01
    关注

    I would like to thanks @kostix and @cloudaice for the answers.

    I figure out what I need to do, I used what @kostix suggested, so I create in each iteration in my for loop, the var manyUsers zendesk.ManyUsers but I also added the follow:

    manyUsers = users[country]
    manyUsers.AppendUsers(mysqlRequest(country, id))
    users[country] = manyUsers
    

    Passing the current value from each country to my manyUsers I am capable of add a new User element in the ManyUsers struct and return the appended value to the users[country]. So, I never aggregate data from iteration and each country gets the correct amount of users.

    Thank you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog