aData := make([]GateData,len(add))
ArrayToStruct(add,aData)
这个是引用的吗。。不用接收返回值,aData 就变了
aData := make([]GateData,len(add))
ArrayToStruct(add,aData)
这个是引用的吗。。不用接收返回值,aData 就变了
|
joymeJan 11, 2020
1
数组类型是比较特殊的,传参的话是数组的内存首地址,
|
|
2
是引用,但是如果切片在 ArrayToStruct 函数内增长,那么 ArrayToStruct 函数调用结束后,aData 指向的切片数据“不是最新的”
|
|
qingsongheJan 11, 2020
3
当然传指针当然是最保险的,可以防止切片在 ArrayToStruct 函数内部增长的问题。
|
|
LancerEvoJan 11, 2020 via iPhone
5
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
感谢各位的解答~~
|
|
cheneydogJan 11, 2020
8
在 go 里数据传递就是拷贝吧,你的代码就是数组传递吧,所以代码错了吧,难道我哪里看错了?
|
|
cheneydogJan 11, 2020
9
确实是切片,糊涂了。
|
|
alexliuxJan 12, 2020 via Android
10
不推荐这样搞。另外,go 没有引用,只有值传递
|