在Go语言中,将数组的指针返回为指针数组可以通过以下方式实现:

huangapple go评论77阅读模式
英文:

return array of pointers from an array in Go

问题

只是让你知道,我对Go语言还很陌生。

我一直在尝试创建一个像这样的函数:

func PointersOf(slice []AnyType) []*AnyType{
    //创建一个指向slice参数中元素的指针切片
}

这相当于对切片中的所有元素执行&slice[idx]操作,但是我在参数和返回类型的类型以及如何创建切片本身方面遇到了问题。

这个方法需要适用于内置类型的切片,以及结构体切片和指向内置类型/结构体的指针切片。

在调用此函数之后,最好不需要对指针切片进行类型转换。

编辑:

我需要这个方法的原因是以一种通用的方式在for...range循环中使用数组的元素,而不是使用元素的副本。请考虑以下示例:

type SomeStruct struct {
    x int
}

func main() {
    strSlice := make([]SomeStruct, 5)
    for _, elem := range strSlice {
        elem.x = 5
    }
}

这段代码无法正常工作,因为elemstrSlice元素的副本。

type SomeStruct struct {
    x int
}

func main() {
    strSlice := make([]SomeStruct, 5)
    for _, elem := range PointersOf(strSlice) {
        (*elem).x = 5
    }
}

然而,这段代码应该可以正常工作,因为你只复制了指向原始数组元素的指针。

英文:

Just so you know, I am quite new to Go.

I have been trying to make a function like this:

func PointersOf(slice []AnyType) []*AnyType{
    //create an slice of pointers to the elements of the slice parameter
}

It is like doing &slice[idx] for all elements in the slice, but I am having trouble with how to type my parameters and return type, as well as how to create the slice itself.

This method needs to work for slices of built-in types, as well as for slices of structs and slices of pointers to built-in types/structs

After invoking this function it would be preferable if I don't have to cast the pointer slice

<hr>

Edit:
The reason why I need this method is to have a generic way to use the elements of an array in a for ... range loop, in stead of using copies of that element. Consider:

type SomeStruct struct {
    x int
}

func main() {
    strSlice := make([]SomeStruct, 5)
    for _, elem := range strSlice {
        elem.x = 5
    }
}

This Doesn't work, because elem is a copy of the strSlice element.

type SomeStruct struct {
    x int
}

func main() {
    strSlice := make([]SomeStruct, 5)
    for _, elem := range PointersOf(strSlice) {
        (*elem).x = 5
    }
}

This however should work, since you only copy the pointer that points to an element in the original array.

答案1

得分: 3

使用以下代码循环遍历结构体切片并设置字段。无需创建指针切片。

type SomeStruct struct {
  x int
}

func main() {
  strSlice := make([]SomeStruct, 5)
  for i := range strSlice {
    strSlice[i].x = 5
  }
}

playground 示例

下面是提议的 PointersOf 函数:

func PointersOf(v interface{}) interface{} {
  in := reflect.ValueOf(v)
  out := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(in.Type().Elem())), in.Len(), in.Len())
  for i := 0; i < in.Len(); i++ {
    out.Index(i).Set(in.Index(i).Addr())
  }
  return out.Interface()
}

以下是如何使用它:

for _, elem := range PointersOf(strSlice).([]*SomeStruct) {
  elem.x = 5
}

playground 示例

英文:

Use the following code to loop through a slice of structs setting a field. There's no need to create a slice of pointers.

type SomeStruct struct {
  x int
}

func main() {
  strSlice := make([]SomeStruct, 5)
  for i := range strSlice {
    strSlice[i].x = 5
  }
}

<kbd>playground example</kbd>

Here's the proposed PointersOf function:

func PointersOf(v interface{}) interface{} {
  in := reflect.ValueOf(v)
  out := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(in.Type().Elem())), in.Len(), in.Len())
  for i := 0; i &lt; in.Len(); i++ {
	out.Index(i).Set(in.Index(i).Addr())
  }
  return out.Interface()
}

and here's how to use it:

for _, elem := range PointersOf(strSlice).([]*SomeStruct) {
	elem.x = 5
}

<kbd>playground example</kbd>

huangapple
  • 本文由 发表于 2016年1月6日 22:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/34635227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定