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:
package main
import "fmt"
func main() {
var myvariable1, myvariable2 int = 89, 45
fmt.Printf( "The value of myvariable1 is : %d\n" ,
myvariable1)
fmt.Printf( "The value of myvariable2 is : %d\n" ,
myvariable2)
}
|
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:
package main
import "fmt"
var myvariable1 int = 100
func main() {
var myvariable2 int = 200
fmt.Printf( "The value of Global myvariable1 is : %d\n" ,
myvariable1)
fmt.Printf( "The value of Local myvariable2 is : %d\n" ,
myvariable2)
display()
}
func display() {
fmt.Printf( "The value of Global myvariable1 is : %d\n" ,
myvariable1)
}
|
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!