跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 问个 Golang 的问题~~
未分類
4 5 月 2020

问个 Golang 的问题~~

问个 Golang 的问题~~

資深大佬 : hcyg 51

aData := make([]GateData,len(add)) ArrayToStruct(add,aData)  

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

大佬有話說 (10)

  • 資深大佬 : joyme

    数组类型是比较特殊的,传参的话是数组的内存首地址,

  • 資深大佬 : qingsonghe

    是引用,但是如果切片在 ArrayToStruct 函数内增长,那么 ArrayToStruct 函数调用结束后,aData 指向的切片数据“不是最新的”

  • 資深大佬 : qingsonghe

    当然传指针当然是最保险的,可以防止切片在 ArrayToStruct 函数内部增长的问题。

  • 資深大佬 : di1012

    @joyme 这个是切片吧,切片传参的确是地址传递。数组是值传递

  • 資深大佬 : LancerEvo

    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

    @di1012 是切片。。。我说错了

  • 主 資深大佬 : hcyg

    感谢各位的解答~~

  • 資深大佬 : cheneydog

    在 go 里数据传递就是拷贝吧,你的代码就是数组传递吧,所以代码错了吧,难道我哪里看错了?

  • 資深大佬 : cheneydog

    确实是切片,糊涂了。

  • 資深大佬 : alexliux

    不推荐这样搞。另外,go 没有引用,只有值传递

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具