Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Golang Global Variables

Introduction to Golang Global Variables

In, Golang Global Variables the Variable which defined at the top of all functions and it is the variable that is outside of all the functions. These variables are available for all the other function, we define a variable as the global when we wanted to have a value common for all the functions and when we wanted to have some value so that it can be reused by many functions. Basically the scope of the global variable is inside the whole program and for all the functions written, and no function can modify the actual global variable instead function can modify the value of the global variable locally inside the function only.

Syntax

In the 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 defining, the only difference is that we cannot define it inside any function, if we define inside any function then the variable will be local to that function, and we can not use it for other functions . Once we define the global variable any function and anywhere inside the program the global variable will be available.

How Global Variables work in Go language?

Before discussing the working of the global variable in the go language we need to understand the uses of the global variable. Suppose you have many functions and all the functions are calculating the rate of interest for the various banks so the rate will differ for all the banks but the principal amount will be same for all the banks hence, for 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 defined at the top of the function. Defining the global variable in 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 value of the global variable inside the other function. We can reassign the global variable and also we define again inside any functions.

Examples to Implement Golang Global Variables

Below are some of the important examples for the global variable and all these variables where we are performing the operations of changing and assigning of the variable and then we are printing. In case if we want to see the output of the below examples we can create a file with name variable.go and copy-paste the example on the file and run the command go run variable.go and we can see the output of the examples.

Example #1

Below is an example where we have created a global variable outside all functions and we 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 are able to get the output as the value of the global variable which 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 are redefining the global variable and assigning the 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 value of the global variable inside the functions and printing the changed value inside the functions. The changed value will reflect inside the function but it will not impact global variable value inside other functions. This shows that making any change into 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 working of the global variable. We learned about the syntax of the global variable along with some of the important examples of the global variable.

Recommended Articles

This is a guide to Golang Global Variables. Here we discuss an introduction to Golang Global Variables, syntax, how does it work with examples. You can also go through our other related articles to learn more –

  1. What is Rust Programming?
  2. Runtime Polymorphism in Java
  3. Python Alternatives
  4. C++ Alternatives

The post Golang Global Variables appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Golang Global Variables

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×