问个 Golang 的问题~~ - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
hcyg
V2EX    Go 编程语言

问个 Golang 的问题~~

  hcyg 2020-01-11 14:43:12 +08:00 4560 次点击
这是一个创建于 2149 天前的主题,其中的信息可能已经有所发展或是发生改变。
aData := make([]GateData,len(add)) ArrayToStruct(add,aData) 

这个是引用的吗。。不用接收返回值,aData 就变了

10 条回复    2020-01-12 09:17:23 +08:00
joyme
    1
joyme  
   2020-01-11 14:59:39 +08:00
数组类型是比较特殊的,传参的话是数组的内存首地址,
qingsonghe
    2
qingsonghe  
   2020-01-11 14:59:49 +08:00   1
是引用,但是如果切片在 ArrayToStruct 函数内增长,那么 ArrayToStruct 函数调用结束后,aData 指向的切片数据“不是最新的”
qingsonghe
    3
qingsonghe  
   2020-01-11 15:01:46 +08:00
当然传指针当然是最保险的,可以防止切片在 ArrayToStruct 函数内部增长的问题。
di1012
    4
di1012  
   2020-01-11 15:10:07 +08:00
@joyme 这个是切片吧,切片传参的确是地址传递。数组是值传递
LancerEvo
    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.
joyme
    6
joyme  
   2020-01-11 15:39:56 +08:00
@di1012 是切片。。。我说错了
hcyg
    7
hcyg  
OP
   2020-01-11 15:40:24 +08:00
感谢各位的解答~~
cheneydog
    8
cheneydog  
   2020-01-11 15:53:06 +08:00
在 go 里数据传递就是拷贝吧,你的代码就是数组传递吧,所以代码错了吧,难道我哪里看错了?
cheneydog
    9
cheneydog  
   2020-01-11 15:55:41 +08:00
确实是切片,糊涂了。
alexliux
    10
alexliux  
   2020-01-12 09:17:23 +08:00 via Android
不推荐这样搞。另外,go 没有引用,只有值传递
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2488 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 24ms UTC 04:39 PVG 12:39 LAX 20:39 JFK 23:39
Do have faith in what you're doing.
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86