2.35. Go language pointer as function parameter

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

Page Views: 9 views

The Go language allows pointers to be passed to functions by setting thepointer type on the parameters defined by the function.

The following example shows how to pass a pointer to a function and modify the value within the function after the function call:

2.35.1. Example #

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 a function to exchange values * &a Address pointing to variable a * &b Address pointing to variable b */ swap(&a, &b); fmt.Printf("The value of a after exchange: %d\\n", a ) fmt.Printf("The value of b after exchange: %d\\n", b ) } func swap(x *int, y *int) { var temp int temp = *x /* Save the value of x address */ *x = *y /* Assign y to x */ *y = temp /* Assign temp to y */ } 

The above example allows the output result to be:

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

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