
aData := make([]GateData,len(add)) ArrayToStruct(add,aData) 这个是引用的吗。。不用接收返回值,aData 就变了
1 joyme 2020-01-11 14:59:39 +08:00 数组类型是比较特殊的,传参的话是数组的内存首地址, |
2 qingsonghe 2020-01-11 14:59:49 +08:00 是引用,但是如果切片在 ArrayToStruct 函数内增长,那么 ArrayToStruct 函数调用结束后,aData 指向的切片数据“不是最新的” |
3 qingsonghe 2020-01-11 15:01:46 +08:00 当然传指针当然是最保险的,可以防止切片在 ArrayToStruct 函数内部增长的问题。 |
5 LancerEvo 2020-01-11 15:17:57 +08:00 via iPhone This is slice not array, and this is how slice looks: type slice struct { Length int Capacity int ZerothElement *byte } A slice contains the length, capacity and a pointer to the zeroth element of the array. When a slice is passed to a function, even though it's passed by value, the pointer variable will refer to the same underlying array. Hence when a slice is passed to a function as parameter, changes made inside the function are visible outside the function too. |
7 hcyg OP 感谢各位的解答~~ |
8 cheneydog 2020-01-11 15:53:06 +08:00 在 go 里数据传递就是拷贝吧,你的代码就是数组传递吧,所以代码错了吧,难道我哪里看错了? |
9 cheneydog 2020-01-11 15:55:41 +08:00 确实是切片,糊涂了。 |
10 alexliux 2020-01-12 09:17:23 +08:00 via Android 不推荐这样搞。另外,go 没有引用,只有值传递 |