Variable scope in Go language

Scope is the range in the source code where the constant, type, variable, function, or package represented by a declared identifier takes effect.

In Go, variables can be declared in three places:

  • Variables defined inside a function are called local variables
  • Variables defined outside a function are called global variables
  • Variables in a function definition are called formal parameters

Next, let's take a closer look at local variables, global variables, and formal parameters.


Local variables

Variables declared inside a function body are called local variables. Their scope is limited to the function body. Parameters and return value variables are also local variables.

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

Example

package main

import "fmt"

func main() {
   /* declare local variables */
   var a, b, c int

   /* initialize parameters */
   a = 10
   b = 20
   c = a + b

   fmt.Printf ("Result: a = %d, b = %d and c = %d\n", a, b, c)
}

The output result of the above example is:

结果: a = 10, b = 20 and c = 30

Global variables

Variables declared outside a function body are called global variables. Global variables can be used throughout the entire package or even in external packages (after being exported).

Global variables can be used in any function. The following example demonstrates how to use global variables:

Example

package main

import "fmt"

/* declare global variables */
var g int

func main() {

   /* declare local variables */
   var a, b int

   /* initialize parameters */
   a = 10
   b = 20
   g = a + b

   fmt.Printf("Result: a = %d, b = %d and g = %d\n", a, b, g)
}

The output result of the above example is:

结果: a = 10, b = 20 and g = 30

In Go language programs, global variables and local variables can have the same name, but local variables inside a function take precedence. Here is an example:

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 result of the above example is:

结果: g = 10

Formal parameters

Formal parameters are used as local variables of the function. Here is an example:

Example

package main

import "fmt"

/* declare global variables */
var a int = 20;

func main() {
   /* declare local variables in the main function */
   var a int = 10
   var b int = 20
   var c int = 0

   fmt.Printf("a = %d in main() function\n",  a);
   c = sum( a, b);
   fmt.Printf("c = %d in main() function\n",  c);
}

/* function definition - add two numbers */
func sum(a, b int) int {
   fmt.Printf("a = %d in sum() function\n",  a);
   fmt.Printf("b = %d in sum() function\n",  b);

   return a + b;
}

The output result of the above example is:

main()函数中 a = 10
sum() 函数中 a = 10
sum() 函数中 b = 20
main()函数中 c = 30

Initializing local and global variables

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

Data types Initialize default values
int 0
float32 0
pointer nil
other extensions