Golang重复使用内存地址复制片? [英] Golang Reusing Memory Address Copying from slice?

查看:104
本文介绍了Golang重复使用内存地址复制片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在进行的一个项目中,我遇到了一个问题。我发现了一个解决方法,但我不确定为什么我的解决方案工作。我希望有人对Go指针的工作方式有更多的经验可以帮助我。



我有一个Model接口和一个实现接口的Region结构。 Model接口在Region结构的指针上实现。我也有一个Regions集合,它是Region对象的一部分。我有一个方法可以将Regions对象转换为[] Model:

  // // Regions是区域模型的集合
类型Regions [] Region

//将模型集合作为模型列表返回
func(coll * Regions)ToModelList()[] Model {
output := make([] Model,len(* coll))
for idx,item:= range * coll {
output [idx] =& item
}
return输出
}

当我运行这段代码时,第一个指向输出多次的区域。所以,如果Regions集合有两个不同的项目,我会得到相同的地址重复两次。当我在变量中设置变量之前打印这些变量时,他们拥有正确的数据。



我搞砸了一点,认为Go可能会重用内存地址在循环之间。此解决方案目前正在为我的测试工作:

  //将模型集合返回为模型列表
func(coll * Regions)ToModelList()[] Model {
output:= make([] Model,len(* coll))
for idx,_:= range * coll {
i :=(* coll)[idx]
输出[idx] =& i
}
返回输出
}
pre>

这给出了输出切片中两个不同地址的预期输出。



这真的好像是一个错误与范围函数重复使用相同的内存地址之间运行,但我总是认为我错过了这样的情况下的东西。



我希望我解释得很好您。我很惊讶,原来的解决方案无法正常工作。



谢谢!

解决方案

在您的第一个(非工作)示例中,项目是循环变量。其地址不变,只是其价值。这就是为什么在输出 idx 次中得到相同地址的原因。



运行此代码以查看运行中的机制;

  func main(){

coll:= [] int {5,10,15}

为i,v:=范围coll {
fmt.Printf(这个总是相同的;%v \ n,& v)
fmt。 Println(这是一个每个迭代大4个字节;%v \ n,& coll [i])
}
}


I was hitting an issue in a project I'm working on. I found a way around it, but I wasn't sure why my solution worked. I'm hoping that someone more experience with how Go pointers work could help me.

I have a Model interface and a Region struct that implements the interface. The Model interface is implemented on the pointer of the Region struct. I also have a Regions collection which is a slice of Region objects. I have a method that can turn a Regions object into a []Model:

// Regions is the collection of the Region model
type Regions []Region

// Returns the model collection as a list of models
func (coll *Regions) ToModelList() []Model {
    output := make([]Model, len(*coll))
    for idx, item := range *coll {
        output[idx] = &item
    }
    return output
}

When I run this code, I end up with the first pointer to the Region outputted multiple times. So, if the Regions collection has two distinct items, I will get the same address duplicated twice. When I print the variables before I set them in the slice, they have the proper data.

I messed with it a little bit, thinking Go might be reusing the memory address between loops. This solution is currently working for me in my tests:

// Returns the model collection as a list of models
func (coll *Regions) ToModelList() []Model {
    output := make([]Model, len(*coll))
    for idx, _ := range *coll {
        i := (*coll)[idx]
        output[idx] = &i
    }
    return output
}

This gives the expected output of two distinct addresses in the output slice.

This honestly seems like a bug with the range function reusing the same memory address between runs, but I always assume I'm missing something in cases like this.

I hope I explained this well enough for you. I'm surprised that the original solution did not work.

Thanks!

解决方案

In your first (non working) example item is the loop variable. Its address is not changing, only its value. That's why you get the same address in output idx times.

Run this code to see the mechanics in action;

func main() {

    coll := []int{5, 10, 15}

    for i, v := range coll {
       fmt.Printf("This one is always the same; %v\n", &v)
       fmt.Println("This one is 4 bytes larger each iteration; %v\n", &coll[i])
    }
}

这篇关于Golang重复使用内存地址复制片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆