Updated May 25, 2023
Introduction to Golang Global Variables
In Golang Global Variables, the variable defined at the top of all functions is outside of all the parts. These variables are available for all the other functions; we define a variable as global when we want to have a value common for all the functions and when we want to have some value so that many functions can reuse it. The global variable’s scope is inside the whole program and for all the functions written, and no function can modify the actual global variable; instead, a function can modify the value of the global variable locally inside the function only.
Syntax
Below, we have written a very simple syntax for the global variable; we can explain the syntax in the following points.
var name-of-variable = assigned any value;
Explanation:
A global variable definition is similar to another variable definition; the only difference is that we cannot define it inside any function; if we define it inside any function, then the variable will be local to that function, and we can not use it for other functions. The global variable will be available once we define the global variable,, any function, and anywhere inside the program.
How do Global Variables work in the Go language?
Before discussing the working of the global variable in the go language, we need to understand the global variable’s uses. Suppose you have many functions, and all the parts are calculating the rate of interest for the various banks so that the rate will differ for all the banks, but the principal amount will be the same for all the banks; hence, the principal amount, we will create a global variable so that all the variable will get the same value for the principal amount. Let me give some important points for explaining the working of the global variable in the go language.
Every global variable in the go language is defined at the top of the function. Defining the global variable in the go language will be similar to that of the local or any other variable; the only difference is that we define it outside the functions. Changing the value of the global variable inside any function will not impact the global variable’s value inside the other function. We can reassign the global variable and define it again inside any function.
Examples to Implement Golang Global Variables
Below are some important examples of the global variable and all these variables. We perform the operations of changing and assigning the variable, and then we print. If we want to see the output of the examples below, we can create a file with a named variable.go and copy-paste the example on the file and run the command go run variable.go, and see the output of the examples.
Example #1
Below is an example where we have created a global variable outside all functions and have assigned a value to it. And we have two functions one is the main function, and another is a normal function called show. Inside these functions, we are printing the value of the global variable, and we can see that we can get the output as the value of the global variable we have defined.
Code:
package main
import "fmt"
//Here we are defining the global variable
var globalvariable int = 111
func main() {
var localvariable int = 222
fmt.Printf("The Global variable glvariable's value is : %d\n",
globalvariable)
fmt.Printf("The Local variable localvariable's value is : %d\n",
localvariable)
show()
}
func show() {
fmt.Printf("The Global variable glvariable's value is : %d\n",
globalvariable)
}
Output:
Example #2
In the below example, we have defined the variable as the global outside the functions and inside the function; we redefine the global variable and assign different values to them. On printing the global variables inside the function, we will get the output from the function as the same, which we have reassigned inside the functions. Which means we can be reassigned to global variables.
Code:
package main
import "fmt"
//Here we are defining the global variable and assigning value to it
var globalvariable int = 111
func main() {
var localvariable int = 222
//Re-assigning the global variable .
var globalvariable int =900
fmt.Printf("The Global variable glvariable's value inside main is : %d\n",
globalvariable)
fmt.Printf("The Local variable localvariable's value is : %d\n",
localvariable)
display()
}
func display() {
var globalvariable int =800
fmt.Printf("The Global variable glvariable's inside function value is : %d\n",
globalvariable)
}
Output:
Example #3
In the below example, we are changing the global variable’s value inside the functions and printing the changed value inside the functions. The changed value will reflect inside the function, but it will not impact the global variable value inside other functions. This shows that making any change to the global variable inside the function will not impact the global variable inside other functions.
Code:
package main
import "fmt"
//Defining the global variable and assigning the value to the variable
var globalvariable int = 111
func main() {
var localvariable int = 222
//Changing the value of the global variable by adding localvariable value
globalvariable := 900+localvariable
fmt.Printf("The Global variable glvariable's value inside main is : %d\n",
globalvariable)
fmt.Printf("The Local variable localvariable's value is : %d\n",
localvariable)
display()
}
func display() {
//Changing the value of the global variable by adding localvariable value
globalvariable := 800+200
fmt.Printf("The Global variable glvariable's inside function value is : %d\n",
globalvariable)
}
Output:
Conclusion
From this tutorial, we learned the basic concept of the global variable in the go language; we also learned the global variable’s working. We learned about the global variable’s syntax and some of the important examples of the global variable.
Recommended Articles
We hope that this EDUCBA information on “Golang Global Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.