WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Scope Rules


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

Variables in the Go language can be declared in three places:

  • The variables defined in the function are called local variables
  • The variables defined outside the function are called global variables
  • The variables in the function definition are called formal parameters

Variables declared in the body of a function are called local variables. Their scope is only in the body of the function. Parameters and return value variables are also local variables.

The main() function in the following example 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 above example execution output is:

global variable

Variables declared outside the function are called global variables, and global variables can be used throughout the package or even outside the package (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

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

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

The above example execution output is:

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

The global variables in the Go language program can be the same as the local variable names, but local variables within the function are given priority. An example is as follows:

Instance

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 above example execution output is:

Results: g =< /span> 10

Formal parameters

Formal parameters are used as local variables of functions. An example is as follows:

example

package main

import "fmt"

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

func main() {
   /* main Function declares a local variable */
   var a int = 10
   var b int = 20
   var c int = 0

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

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

   return a + b;
}

The above example execution output is:

main()Function a = 10
sum() Function a = 10
sum() Function b = 20
main()Function c = 30

Initialize local and global variables

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

Data Type Initialize defaults
int 0
float32 0
pointer nil