Scope of Variables in Go
Prerequisite: Variables in Go Programming Language
The Scope of a variable can be defined as a part of the program where a particular variable is accessible. A variable can be defined in a class, method, loop, etc. Like C/C++, in Golang all identifiers are lexically (or statically) scoped, i.e. scope of a variable can be determined at compile time. Or you can say a variable can only be called from within the block of code in which it is defined.
Golang scope rules of variables can be divided into two categories which depend on where the variables are declared:
- Local Variables(Declared Inside a block or a function)
- Global Variables(Declared outside a block or a function)
Local Variables
- Variables that are declared inside a function or a block are termed as Local variables. These are not accessible outside the function or block.
- These variables can also be declared inside the for, while statement etc. inside a function.
- However, these variables can be accessed by the nested code blocks inside a function.
- These variables are also termed as the block variables.
- There will be a compile-time error if these variables are declared twice with the same name in the same scope.
- These variables don’t exist after the function’s execution is over.
- The variable which is declared outside the loop is also accessible within the nested loops. It means a global variable will be accessible to the methods and all loops. The local variable will be accessible to loop and function inside that function.
- A variable which is declared inside a loop body will not be visible to the outside of loop body.
Example:
// Go program to illustrate the // local variables package main import "fmt" // main function func main() { // from here local level scope of main function starts // local variables inside the main function var myvariable1, myvariable2 int = 89, 45 // Display the values of the variables fmt.Printf( "The value of myvariable1 is : %d\n" , myvariable1) fmt.Printf( "The value of myvariable2 is : %d\n" , myvariable2) } // here local level scope of main function ends |
Output:
The value of myvariable1 is : 89 The value of myvariable2 is : 45
Global Variables
- The variables which are defined outside of a function or a block is termed as Global variables.
- These are available throughout the lifetime of a program.
- These are declared at the top of the program outside all of the functions or blocks.
- These can be accessed from any portion of the program.
Example:
// Go program to illustrate the // global variables package main import "fmt" // global variable declaration var myvariable1 int = 100 func main() { // from here local level scope starts // local variables inside the main function var myvariable2 int = 200 // Display the value of global variable fmt.Printf( "The value of Global myvariable1 is : %d\n" , myvariable1) // Display the value of local variable fmt.Printf( "The value of Local myvariable2 is : %d\n" , myvariable2) // calling the function display() } // here local level scope ends // taking a function func display() { // local level starts // Display the value of global variable fmt.Printf( "The value of Global myvariable1 is : %d\n" , myvariable1) } // local scope ends here |
Output:
The value of Global myvariable1 is : 100 The value of Local myvariable2 is : 200 The value of Global myvariable1 is : 100
Note: What will happen there exists a local variable with the same name as that of the global variable inside a function?
The answer is simple i.e. compiler will give preference to the local variable. Usually when two variable with the same name is defined then the compiler produces a compile-time error. But if the variables are defined in different scopes then the compiler allows it. Whenever there is a local variable defined with the same name as that of a global variable then the compiler will give precedence to the local variable.
- Example: In the below program, you can clearly see the output. As myvariable1 value is 200 which is giving in function main. So you can say a local variable has a high preference over a global variable.
// Go program to show compiler giving preference
// to a local variable over a global variable
package main
import
"fmt"
// global variable declaration
var myvariable1
int
= 100
func main() {
// from here local level scope starts
// local variables inside the main function
// it is same as global variable
var myvariable1
int
= 200
// Display the value
fmt.Printf(
"The value of myvariable1 is : %d\n"
,
myvariable1)
}
// here local level scope ends
Output:
The value of myvariable1 is : 200
Please Login to comment...