doucuan5365 2017-01-26 20:33
浏览 43
已采纳

如何在golang中将地图正确解压缩到自定义嵌套结构中(aws sdk-for-go)

I have a map that contains two values (Etag & PartNumber) as follows:

upload_out := make(map[int64]string) //key - PartNumber, value - Etag

i eventually want to loop through this map with my values and dump them into into a slice of custom struct as follows:

Parts: []*s3.CompletedPart{
        { // Required
            ETag:       aws.String("ETag1"),
            PartNumber: aws.Int64(PartNumber1),
        },
        { // Required
            ETag:       aws.String("ETag2"),
            PartNumber: aws.Int64(PartNumber2),
        },
        // More values...
    },

I guess my problem is not understanding how to properly do this. My attempt loop below only adds one key, value pair all the time. So not all values are being unpacked.

var paths  []*s3.CompletedPart
    for key, val := range upload_out {
        //var unique [10000]*s3.CompletedPart //Attempt unique variable names

         name :=  &s3.CompletedPart{ // this only does one
            ETag:       &val,
            PartNumber: &key,
        }


        paths = append(paths, name)
    }

Any help doing this right will be appreciated.

  • 写回答

1条回答 默认 最新

  • dongyi5070 2017-01-26 20:37
    关注

    You are assigning the address of the key and val variables in your loop, which there is only ever one of each. You need to copy those values so you can assign a new pointer for each entry. The easiest way is to use the provided aws convenience functions:

    for key, val := range upload_out {
        name := &s3.CompletedPart{
            ETag:       aws.String(val),
            PartNumber: aws.Int64(key),
        }
        paths = append(paths, name)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路