A scope in Golang is the region of a program where a defined variable can exist, and beyond that, a variable cannot be accessed. The variable’s scope can be defined as a part of the program where the specific variable is accessible. A variable can be defined in the class, method, loop, etc. Just Like C/C++, in Go, all identifiers are lexically (or statically) scoped; for example, the scope of the variable can be determined at compile time.
Scope of Variables In Golang
There are three places where the variables can be declared in Golang.
- Local variables
- Global variables
- Formal parameters
Let us deep dive into what are local and global variables and what formal parameters are.
Local variables in Golang
Local variables are those declared inside a function or a block. Local variables can be used only by statements that are inside that function or block of code.
However, these variables can be accessed by a nested code block inside the function.
These variables are also called block variables.
There will be a compile-time error if these variables are declared twice with the same name in the same scope.
The variable which is declared outside the for loop is also accessible within the nested loops. It means the global variable will be accessible to the methods.
The local variable will be accessible to a loop and function inside that function.
See the following code example.
// hello.go package main import "fmt" func main() { var GenY, GenZ int = 21, 11 fmt.Printf("The value of GenY is : %d\n", GenY) fmt.Printf("The value of GenZ is : %d\n", GenZ) }
Output
go run hello.go The value of GenY is : 21 The value of GenZ is : 11
In the above code, we have defined two variables inside the main() function. Local variables are not known to functions outside their own.
Global variables in Golang
Global variables are defined outside of the function, usually on top of the program.
They hold their value throughout the program’s lifetime, and they can be accessed inside any of the functions defined for a program.
In short, the variables which are defined outside of the function or a block is termed as Global variables. These are available throughout the lifetime of a program.
Global variables are declared at the top of the program outside all of the functions or blocks.
Global variables can be accessed from any portion of the program.
See the following code.
// hello.go package main import "fmt" /* global variable declaration */ var s int func main() { /* local variable declaration */ var r, h int /* actual initialization */ r = 11 h = 21 s = r + h fmt.Printf("value of r = %d\n", r) fmt.Printf("value of h = %d\n", h) fmt.Printf("value of s = %d\n", s) }
Output
go run hello.go value of r = 11 value of h = 21 value of s = 32
What will happen if a local variable with the same name as that of the global variable inside the function?
The answer is simple. The compiler will give the preference to a local variable.
Usually when two variable with the same name is defined, then a 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.
See the following code.
// hello.go package main import "fmt" /* global variable declaration */ var k int = 11 func main() { /* local variable declaration */ var k, b int /* initialization */ k = 19 b = 21 fmt.Printf("value of k = %d\n", k) fmt.Printf("value of b = %d\n", b) }
Output
go run hello.go value of k = 19 value of b = 21
Formal Parameters in Golang
Formal parameters are treated as the local variables within that function, and they take preference over the global variables.
See the following code for more understanding.
// hello.go package main import "fmt" /* global variable declaration */ var x int = 20 func main() { /* local variable declaration in main function */ var x int = 10 var y int = 20 var z int fmt.Printf("value of x in main() function = %d\n", x) z = sum(x, y) fmt.Printf("value of z in main() function = %d\n", z) } /* function to add two integers */ func sum(a, b int) int { fmt.Printf("value of a in sum() function = %d\n", a) fmt.Printf("value of b in sum() function = %d\n", b) return a + b }
Output
go run hello.go value of x in main() function = 10 value of a in sum() function = 10 value of b in sum() function = 20 value of z in main() function = 30
In the above example, we have defined x in the global and local scope.
When we call the function, it will pass the value of local scope and not the global scope; that is why the output value of z is 30 and not 40.
Initializing Local and Global Variables in Go
Local and global variables are initialized to their default value, which is 0;
Go Pointers are initialized to nil.
Conclusion
Scoping is linked closely with a declaration of identifier (being more precise with where an identifier is declared).
The scope of an identifier is a part of the source code (or even all) where the identifier binds to specific values like a variable, constant, package, etc.
Finally, the Scope of Variables In Golang example is over.