2.35. Go language pointer as function parameter

发布时间 :2023-10-12 23:00:09 UTC      

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.