Scope of variable in Go program

Scope of the variable in the Go program: The scope of a variable is defined from the declaration of the variable in the go program.  It is the region where a variable can be accessed in the go program. All identifiers are statically scoped in the go language, which means all variable's scope will be decided at the compilation time.

All variables have a specific area in which they can be accessed. The compiler decides this area at the time of the compilation in the go program.

On the basis of the scope, there will be two categories of variables in the go language:

  1. Local variable
  2. Global variable

Local variable:

Local variables are those variables that are defined inside a block or function. You can't access the local variable outside of the function. Or you can say that the variables defined inside the curly braces are known as a local variable; that's why local variables are also known as the block variable.

Here some key points about the local variables are given below:

  1. Local variables can also be defined inside of the loops like for, while, etc.
  2. If a variable is declared inside a loop, then it will not be accessed outside of the loop.
  3. The local variables can be accessed in the nested functions. If a function is defined inside a function, then the local variable declared in the parent function will be accessible by his child function.
  4. The local variables can be used inside the function only.
  5. Once the function is completely executed, then the local variables related to that function will no more exist in the go program.
  6. Two variables with the same name can't be declared in the same function, but two variables with the same name can be declared in a different function in a go program.

Program:

Here is a program of declaring a local variable is given below:

 package main
 // scope of local variable
 import "fmt"
 func main() {  // Local variable's scope for main func starts from here.
             var num int = 10       // It is a local variable.
             fmt.Printf("local variable’s value is : %d \n", num)
             dummy()                     // function calling
 }           // Local variable's scope ends here.
 // 2nd function
 func dummy(){           // Local variable's scope for dummy func starts from here.
             var num int = 20       // it is a local variable.
             fmt.Printf("local variable’s value is : %d", num)
 }           // Local variable's scope ends here. 

Output:

Global variable:

The global variables are those variables that are defined outside of the function in the go program. The developer can access these variables anywhere in the go program.

Global variables are declared at the top of the program, outside of all blocks and before declaring all functions. These variables will be placed just after the import statement in the go program.

Here, some key points about the global variables are given below:

  1. There is no specific keyword to define a global variable in the go language; it requires a normal variable declaration format only.
  2. The global variables are accessible by all functions defined in the go program.
  3. If a nested function is declared, then the parent function's local variable will act as a global variable for his child function.
  4. You can give the same name to the global variable and local variable of a function in the go program. Still, in this case, the function will prioritize his local variable over the global variable and perform all operations on his local variable's value.
  5. The global variables have lifetime availability in the go program means they will exist at the end of the execution process of the go program.
  6. You can access these variables anytime throughout the program.

Program:

Here program declaring a global variable and their accessing inside a function are given below:

 package main
 // Scope of Global variable
 import "fmt"
 var num int = 10       // it is a global variable.
 func main() {  // Local variable's scope for main func starts from here.
             var num1 int = 20     // it is a local variable.
             fmt.Printf("Global variable’s value is : %d \n", num)
             fmt.Printf("local variable’s value is : %d \n\n", num1)
             // function calling
             dummy()
             dummy1()
 }           // Local variable's scope ends here.
 // 2nd function
 func dummy(){           // Local variable's scope for dummy func starts from here.
             var num1 float32 = 20.5
             fmt.Printf("local variable’s value is : %f \n", num1)
             fmt.Printf("Global variable’s value is : %d \n\n", num)
 }           // Local variable's scope ends here.
 func dummy1(){         // Local variable's scope for dummy1 func starts from here.
             var num int = 1234
             fmt.Printf("The value of variable is: %d", num)
 }           // Local variable's scope ends here. 

Output:

Scope of variable in Go program



ADVERTISEMENT
ADVERTISEMENT