dongxi3911 2018-08-01 08:26
浏览 52
已采纳

连接来自作为接口传递的指针的切片

I'm working on a client for a specific API, and I built several structures (one for each route).
The API works with pages so I have getters returning slices of a struct. This is done passing through a common function that takes an interface supposed to be a pointer to a slice of the wanted structure so I can use Unmarshal from the json package.

The problem I'm facing is that I would like to get all the pages in a single request, but in order to do that I would need to concatenate several slices.

I tried to do this using reflect but I can only manage to create a new slice without actually altering the value that my interface is pointing to.

Here's a part of my code:

func concatenateSlice(dst, src interface{})(){
    dstValue := reflect.ValueOf(dst)
    srcValue := reflect.ValueOf(src)
    if srcValue.Type() != dstValue.Type(){
        return
    }
    switch srcValue.Kind(){
    case reflect.Ptr, reflect.Slice:
        dstValuePoint := reflect.Indirect(dstValue)
        srcValuePoint := reflect.Indirect(srcValue)
        reflect.Copy(dstValuePoint, reflect.AppendSlice(dstValuePoint, srcValuePoint))
    }
}

I'm sorry for the sloppy post, this is my first time on stackoverflow.

  • 写回答

1条回答 默认 最新

  • dsp1836 2018-08-01 08:47
    关注

    You shouldn't use reflect.Copy() to copy the result slice (after append), as destination will not have enough space. reflect.AppendSlice() will not change the slice pointed by dst, it will allocate a new one if needed, and it returns the new slice (more precisely the reflect.Value holding it).

    What you should do is set the slice returned by reflect.AppendSlice() to the value pointed by dst, simply using the Value.Set() method. One thing to note is that you are not setting a slice pointer but a slice, so first call e.g. Value.Elem() before calling Set().

    See this example:

    func concatenateSlice(dst, src interface{}) {
        dv := reflect.ValueOf(dst)
        sv := reflect.ValueOf(src)
        if dv.Type() != sv.Type() {
            return
        }
    
        dv2 := reflect.AppendSlice(dv.Elem(), sv.Elem())
        dv.Elem().Set(dv2)
    }
    

    Testing it:

    dst := []int{1, 2}
    src := []int{3, 4}
    
    concatenateSlice(&dst, &src)
    
    fmt.Println(dst)
    

    Output (try it on the Go Playground):

    [1 2 3 4]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像