Golang中参数传递详解

来源:互联网 发布:洞主手工皂淘宝在哪 编辑:程序博客网 时间:2024/06/02 06:42

关于参数传递,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数据结构如下:

image

示例代码:
package mainimport "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 mainfunc main() {    value := new(int)    modifyFunc(value)    println("main:", value)}func modifyFunc(value *int) {    value = nil    println("modifyFunc:", value)}

执行结果:

modifyFunc: 0x0main: 0xc820049f30

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