Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 从指向类型的指针创建类型的切片_Go - Fatal编程技术网

Go 从指向类型的指针创建类型的切片

Go 从指向类型的指针创建类型的切片,go,Go,尝试创建一个切片,其中类型是基于指向特定类型的指针动态设置的,因此我制作了以下示例 func main() { var chicken *Chicken //create a slice of chickens chickens:=GetaDynamiclyTypedSlice(chicken) //this throws cannot range over chickens (type *[]interface {}) and i cant figure h

尝试创建一个切片,其中类型是基于指向特定类型的指针动态设置的,因此我制作了以下示例

func main() {
    var chicken *Chicken
    //create a slice of chickens
    chickens:=GetaDynamiclyTypedSlice(chicken)

    //this throws  cannot range over chickens (type *[]interface {}) and i cant figure how to create a slice using my above chicken pointer
    for _,chicken := range chickens{
        fmt.Println(chicken)
    }

}

type Chicken struct{
    Weight float64
}

func GetaDynamiclyTypedSlice(ptrItemType interface{})*[]interface {}{
    var collection []interface{}
    itemtyp := reflect.TypeOf(ptrItemType).Elem()
    for i:=0;i<1000;i++{
        //create an item of the wanted type
        item := reflect.New(itemtyp)
        //set a random float to the weight value
        item.Elem().FieldByName("Weight").SetFloat(rnd.ExpFloat64())
        collection = append(collection,&item)
    }
    return &collection
}
我应该怎么做才能使用返回切片上的范围? 如何使用itemtyp作为切片的类型?
您只需要取消对指针的引用,这样您就不会在指针上迭代—而是在切片上迭代:

for _, chicken := range *chickens {
    // ...
}

操场链接:

您只需取消对指针的引用,这样您就不会在指针上迭代-您正在迭代一个切片:

for _, chicken := range *chickens {
    // ...
}

操场链接:

您的代码几乎没有问题

您正在返回一个指向reflect.Value的指针,99%确定这不是您想要实现的

你没有像西蒙提到的那样取消对切片的引用

片是指针类型,如果出于性能原因返回*[]接口{},那么实际上是在伤害而不是帮助

因此,让我们重写代码并对其进行优化!现在已经很晚了,所以,派对时间到了:

// pass the size to preallocate the slice, also return the correct slice type.
func GetaDynamiclyTypedSlice(ptrItemType interface{}, size int) (col []interface{}) {
    col = make([]interface{}, size)
    itemtyp := reflect.TypeOf(ptrItemType).Elem()
    for i := range col { //prettier than for i := 0; etc etc
        item := reflect.New(itemtyp)
        item.Elem().FieldByName("Weight").SetFloat(rand.ExpFloat64())
        col[i] = item.Interface() //this is the magic word, return the actual item, not reflect.Value
    }
    return
}

您的代码几乎没有问题

您正在返回一个指向reflect.Value的指针,99%确定这不是您想要实现的

你没有像西蒙提到的那样取消对切片的引用

片是指针类型,如果出于性能原因返回*[]接口{},那么实际上是在伤害而不是帮助

因此,让我们重写代码并对其进行优化!现在已经很晚了,所以,派对时间到了:

// pass the size to preallocate the slice, also return the correct slice type.
func GetaDynamiclyTypedSlice(ptrItemType interface{}, size int) (col []interface{}) {
    col = make([]interface{}, size)
    itemtyp := reflect.TypeOf(ptrItemType).Elem()
    for i := range col { //prettier than for i := 0; etc etc
        item := reflect.New(itemtyp)
        item.Elem().FieldByName("Weight").SetFloat(rand.ExpFloat64())
        col[i] = item.Interface() //this is the magic word, return the actual item, not reflect.Value
    }
    return
}

1。我甚至没有考虑性能问题。你所在的聚会时间是我所在的工作日的中间时间…:早上4:36,总是在睡前的派对时间。@OnePartyWalla s7oor?哈哈哈派对的意思是看夏洛克和回答问题,所以现在对s7oor来说太晚了。@OneOfOne ya mawlana哈哈+1。我甚至没有考虑性能问题。你所在的聚会时间是我所在的工作日的中间时间…:早上4:36,总是在睡前的派对时间。@OnePartyWalla s7oor?哈哈哈派对的意思是看夏洛克和回答问题,所以,现在对s7oor来说太晚了。@OneOfOne ya mawlana哈哈