加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang中参数传递详解

发布时间:2020-12-16 18:06:22 所属栏目:大数据 来源:网络整理
导读:本文原创文章,转载注明出处,博客地址 https://segmentfault.com/u/to... 第一时间看后续精彩文章。觉得好的话,顺手分享到朋友圈吧,感谢支持。 关于参数传递,Golang文档中有这么一句: after they are evaluated,the parameters of the call are passed by val

本文原创文章,转载注明出处,博客地址 https://segmentfault.com/u/to... 第一时间看后续精彩文章。觉得好的话,顺手分享到朋友圈吧,感谢支持。

关于参数传递,Golang文档中有这么一句:

after they are evaluated,the parameters of the call are passed by value to the

function and the called function begins execution.

函数调用参数均为值传递,不是指针传递或引用传递。经测试引申出来,当参数变量为指针或隐式指针类型,参数传递方式也是传值(指针本身的copy)

Slice是最常用的数据结构之一,下面以Slice为例,解释Golang的参数传递机制。

Slice数据结构如下:

示例代码:
package main

import "fmt"

func main(){
    slice := make([]int,3,5)
    fmt.Println("before:",slice)
    changeSliceMember(slice)
    fmt.Println("after:",slice)
}

func changeSliceMember(slice []int) {
    if len(slice) > 1 {
    slice[0] = 9
    }
}

函数执行结果为:

befor:[0 0 0]
after:[9 0 0]
解释:

从数据结构图中可看出,Slice可以理解成结构体类型,包含底层数组首元素地址、数组len、容量三个字段,slice对象在参数传值过程中,把三个字段的值传递过去了,实际上changeSliceMember函数内slice在内存中的地址和main中的slice内存地址不一样,只是字段值是一样的,而第一个字段Pointer的值就是底层数组首元素地址,因此可以直接改变元素内容

可以与下面代码做对比,理解:
package main

func main() {
    value := new(int)
    modifyFunc(value)
    println("main:",value)
}

func modifyFunc(value *int) {
    value = nil
    println("modifyFunc:",value)
}

执行结果:

modifyFunc: 0x0
main: 0xc820049f30

可以看出,即使传值为指针,仍未改变变量value在main中的值,因为modifyFunc中value的值为指针,和main中的value值一样,但是俩对象本身是两个对象,读者可以细细体会

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读