2.12. Go language function reference pass value

发布时间 : 2025-10-25 13:32:53 UTC      

Page Views: 9 views

Reference passing means that the address of the actual parameter is passed to the function when the function is called, so the modification of the parameter in the function will affect the actual parameter.

The reference passes the pointer parameter to the function, and here is the exchange function swap() reference was used to pass:

/* Defining Exchange Value Functions*/ func swap(x *int, y *int) { var temp int temp = *x /* Maintain the value on the x address */ *x = *y /* Assign y value to x */ *y = temp /* Assign the temp value to y */ } 

We call the following by using reference passing swap() function:

package main import "fmt" func main() { /* Define local variables */ var a int = 100 var b int= 200 fmt.Printf("Value of a before exchange : %d\n", a ) fmt.Printf("Value of b before exchange: %d\n", b ) /* Calling the swap () function * &A points to the pointer a, and the address of the variable a * &b Pointer to b, address of b variable */ swap(&a, &b) fmt.Printf("After exchange, the value of a : %d\n", a ) fmt.Printf("After exchange, the value of b : %d\n", b ) } func swap(x *int, y *int) { var temp int temp = *x /* Save values on address x */ *x = *y /* Assign y value to x*/ *y = temp /* Assign the temp value to y*/ } 

The result of the above code execution is:

Value of a before exchange: 100 Value of b before exchange: 200 After exchange, the value of a: 200 After exchange, the value of b: 100 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。