Go language variable scope


Release date:2023-09-12 Update date:2023-10-13 Editor:admin View counts:442

Label:

Go language variable scope

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.

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

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

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

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

Powered by TorCMS (https://github.com/bukun/TorCMS).