2.16. Go language variable scope

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

Page Views: 10 views

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

《地理信息系统原理、技术与方法》  97

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