duanjiushu5063 2014-11-03 20:30
浏览 300
已采纳

在golang中将interface {}转换为* [] int

I receive an interface which is basically a slice. Now I want to convert it to a pointer to the slice. The problem is, that I have either the slice itself or a Pointer to an interface. I can easily show in a code example:

func main(){
    model := []int{1,2,3,4,5,6,7,8,10,11,133123123123}
    method(model)
}

func method(model interface{}){ 
    fmt.Println(reflect.TypeOf(model))    // this is of type []int
    fmt.Println(reflect.TypeOf(&model))   // this is of type *interface{}
}

What I need is this type:

fmt.Println(reflect.TypeOf(result))    // this should be type *[]int

I know the type only on runtime, therefore I cannot just take &(model.([]int))

Is there a way using golang reflection to receive this? the type 'int' is here actually not important, important is, that it is a Pointer to a slice. *[]interface{} would be okay either.

Edit:
To make the question more clear, I should have added: I am not interested in the data of the slice, but only in getting a pointer to a slice of same type (which can basically be empty). Therefore James Henstridge answers works perfectly.

  • 写回答

1条回答 默认 最新

  • dousha2020 2014-11-04 04:03
    关注

    Before trying to answer the question, it is worth stepping back and asking what the *[]int value you're after should point at?

    Given the way method is called we can't possibly get a pointer to the model variable from the calling context, since it will only receive a copy of the slice as its argument (note that this is a copy of the slice header: the backing array is shared).

    We also can't get a pointer to the copy passed as an argument since it is stored as an interface{} variable: the interface variable owns the memory used to store its dynamic value, and is free to reuse it when the a new value is assigned to it. If you could take a pointer to the dynamic value, this would break type safety if a different type is assigned.

    We can obtain a *[]int pointer if we make a third copy of the slice, but it isn't clear whether that's what you'd necessarily want either:

    v := reflect.New(reflect.TypeOf(model))
    v.Elem().Set(reflect.ValueOf(model))
    result := v.Interface()
    

    This is essentially a type agnostic way of writing the following:

    v := new([]int)
    *v = model
    var result interface{} = v
    

    Now if you really wanted a pointer to the slice variable in the calling context, you will need to ensure that method is called with a pointer to the slice instead and act accordingly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接