SoFunction
Updated on 2025-03-02

Detailed explanation of Go variable scope

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

Variables in Go 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 us understand local variables, global variables and formal parameters in detail.

Local variables

Variables declared in the function body are called local variables, and their scope is only within the function body, and parameter and return value variables are also local variables.

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

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

The output results of the above example execution are:

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

Global variables

Variables declared in vitro of the function are called global variables, which can be used throughout the entire package or even external packages (after export).

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

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

The output results of the above example execution are:

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

Global variables and local variable names in Go language programs can be the same as local variables, but local variables in functions will be given priority. Examples are as follows:

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

The output results of the above example execution are:

Results: g = 10

Formal parameters

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

package main
 
import "fmt"
 
/* Declare global variables */
var a int = 20;
 
func main() {
   /* Declare local variables in main function */
   var a int = 10
   var b int = 20
   var c int = 0
 
   ("main()In the function a = %d\n",  a);
   c = sum( a, b);
   ("main()In the function c = %d\n",  c);
}
 
/* Function definition - Add two numbers */
func sum(a, b int) int {
   ("sum() In the function a = %d\n",  a);
   ("sum() In the function b = %d\n",  b);
 
   return a + b;
}

The output results of the above example execution are:

in main() function a = 10
in sum() function a = 10
b = 20 in sum() function
c = 30 in main() function

Initialize local and global variables

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

Data Type Initialize the default value
int 0
float32 0
pointer nil

Use formal parameters to compare a in the sum function and a in the main function. Although 1 is added to the sum function, the original value in main is still 10:

package main
 
import "fmt"
 
/* Declare global variables */
var a int = 20
 
func main() {
    /* Declare local variables in main function */
    var a int = 10
    var b int = 20
    var c int = 0
 
    ("main()In the function a = %d\n", a)
    c = sum(a, b)
    ("main()In the function a = %d\n", a)
    ("main()In the function c = %d\n", c)
}
 
/* Function definition - Add two numbers */
func sum(a, b int) int {
    a = a + 1
    ("sum() In the function a = %d\n", a)
    ("sum() In the function b = %d\n", b)
    return a + b
}

The output is:

in main() function a = 10
in sum() function a = 11
b = 20 in sum() function
in main() function a = 10
c = 31 in main() function

The above is a detailed explanation of the scope of Go language variables. For more information about the scope of Go language variables, please pay attention to my other related articles!