Go语言按值调用函数

通过传递参数到函数的方法的调用是指将参数的实际值复制到函数的形式参数中。 在这种情况下,在函数中对参数所做的更改不会影响参数值。

默认情况下,Go编程语言使用按值调用方法传递参数。 一般来说,函数中的代码不能改变传入函数的参数。参考函数swap()定义如下。

/* function definition to swap the values */
func swap(int x, int y) int {
   var temp int

   temp = x /* save the value of x */
   x = y    /* put y into x */
   y = temp /* put temp into y */

   return temp;
}

现在,通过传递实际值来调用函数swap(),完整的代码如下例所示:

package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100
   var b int = 200

   fmt.Printf("Before swap, value of a : %d\n", a )
   fmt.Printf("Before swap, value of b : %d\n", b )

   /* calling a function to swap the values */
   swap(a, b)

   fmt.Printf("After swap, value of a : %d\n", a )
   fmt.Printf("After swap, value of b : %d\n", b )
}
func swap(x, y int) int {
   var temp int

   temp = x /* save the value of x */
   x = y    /* put y into x */
   y = temp /* put temp into y */

   return temp;
}

把上面的代码放在一个单独的go文件中,编译并执行它,它会产生以下结果:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200

这表明,虽然参数在函数内部已更改,但参数的值在外部并没有更改。


上一篇: Go语言函数 下一篇: Go语言作用域规则