2.16. Go language variable scope

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

The scope is the scope of the constant, type, variable, function, or packagerepresented by the declared identifier in the source code.

Go variables in a language can be declared in three places:

  • The variables defined in the function are called local variables.

  • Variables defined outside the function are called global variables

  • Variables in function definitions are called formal parameters

Next, let’s look at local variables, global variables, and formal parametersin detail.

2.16.1. Local variable #

The variables declared in the body of the function are called local variables, their scope is only in the body of the function, and the parameters and return value variables are also local variables.

In the following example, the main() function uses local variables a , b , c :

Example #

package main
import "fmt"
func main() {
   /* Declare local variables */
   var a, b, c int
   /* Initialization parameters */
   a = 10
   b = 20
   c = a + b
   fmt.Printf ("result: a = %d, b = %d and c = %d\\n", a, b, c)
}

The output of the above example is as follows:

result: a = 10, b = 20 and c = 30

2.16.2. Global variable #

Variables declared outside the function are called global variables, which can be used in the entire package or even the external package (after being exported).

Global variables can be used in any function, and the following example demonstrates how to use global variables:

Example #

package main
import "fmt"
/* Declare global variables */
var g int
func main() {
   /* Declare global variables */
   var a, b int
   /* Initialization parameter */
   a = 10
   b = 20
   g = a + b
   fmt.Printf("result: a = %d, b = %d and g = %d\\n", a, b, g)
}

The output of the above example is as follows:

result: a = 10, b = 20 and g = 30

Go language program, the names of global and local variables can bethe same, but local variables within the function are preferred. Examples are as follows:

Example #

package main
import "fmt"
/* Declare global variables */
var g int = 20
func main() {
   /* Declare local variables */
   var g int = 10
   fmt.Printf ("result: g = %d\\n",  g)
}

The output of the above example is as follows:

result: g = 10

2.16.3. Formal parameter #

Formal arguments are used as local variables of the function. Examples are as follows:

Example #

package main
import "fmt"
/* Declare global variables */
var a int = 20;
func main() {
   /* Declaring local variables in the main function */
   var a int = 10
   var b int = 20
   var c int = 0
   fmt.Printf("main()In the function a = %d\\n",  a);
   c = sum( a, b);
   fmt.Printf("main()In the function c = %d\\n",  c);
}
/* Function Definition - Adding Two Numbers*/
func sum(a, b int) int {
   fmt.Printf("sum() In the function a = %d\\n",  a);
   fmt.Printf("sum() In the function b = %d\\n",  b);
   return a + b;
}

The output of the above example is as follows:

In the main() function, a=10
In the sum() function, a=10
B=20 in the sum() function
C=30 in the main() function

2.16.4. Initialize local and global variables #

The default values for different types of local and global variables are:

Data type

Initialize default

Int

0

Float32

0

Pointer

Nil

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.