Golang中参数传递详解

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

关于参数传递,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的参数传递机制。lua

Slice数据结构以下:

image

示例代码:
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
    }
}

函数执行结果为:spa

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)
}

执行结果:code

modifyFunc: 0x0
main: 0xc820049f30

能够看出,即便传值为指针,仍未改变变量value在main中的值,由于modifyFunc中value的值为指针,和main中的value值同样,可是俩对象自己是两个对象,读者能够细细体会对象

相关文章
相关标签/搜索