Understanding Golang Global Variables

Global variables in Go are “declared outside of any function and accessible from any function within the same package”. Global variables are often used to store data shared by multiple functions. For example, a global variable can store the user’s name or the current time.

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.

How to create a global variable in Golang?

You can create a global variable in Go by “declaring a variable outside any function”. Global variables are declared at the program’s top before the main() function.

Syntax

var name_of_variable = your_value

You cannot define a global variable inside any function; if we define it inside any function, the variable will be local to that function, and we can not use it for other functions.

Example 1: Create and print the global variable

package main

import "fmt"

var globalVar int = 21

func main() {
  fmt.Println("The value of globalVar is:", globalVar)
}

Output

The value of globalVar is: 21

In this example, the global variable globalVar is declared at the package level and can be accessed by the main function.

The value of globalVar can be changed by any function in the same package, which makes it a powerful mechanism for sharing data between different parts of the program.

Example 2: Global Variables are mutable

package main

import "fmt"

var x = 21

func main() {
  fmt.Println(x)
  multiply()
}

func multiply() {
  fmt.Println(x * 2)
}

Output

21
42

Global variable and Local variable

The main difference between global and local variables in Golang is that a global variable is a variable that is declared outside of any function and accessible from any function within the package. In contrast, a local variable is a variable that is declared within a function and can only be accessed within that function.

Global variables store the data needed by multiple functions or provide a way of sharing data between different program parts. In contrast, local variables store the data detailed to a single function and are not needed by other functions.

Example

package main

import "fmt"

var globalVar int = 21

func main() {
  fmt.Println("The value of global variable is:", globalVar)
  localVar := 19
  fmt.Println("The value of local variable is:", localVar)
}

Output

The value of global variable is: 21
The value of local variable is: 19

In this example, the globalVariable is declared at the package level and is accessible by the main() function.

The localVariable is declared within the main() function and can only be accessed within that function.

Global Variables – Is it helpful or not?

Global variables, while sometimes necessary, can indeed cause issues in a program, especially in a large one where many functions might interact with the variable. The problems that can arise due to using global variables include:

  1. Unwanted side-effects: Global variables can be changed by any part of the program. This could lead to unexpected behavior if a function modifies a global variable that another part of the program relies on.

  2. Difficult debugging: When a global variable is causing a bug, it can be hard to find where the problem comes from since any part of the program could be the culprit.
  3. Concurrency issues: If you’re writing a multithreaded program, you must carefully control access to global variables to avoid race conditions, where the order of operations could affect the result.
  4. Namespace pollution: Many global variables can clutter the global namespace, increasing the chance of name conflicts.

The only time to use global variables is when it’s a constant, and many functions will use it. For example, PI or natural log base.

Leave a Comment