Go Variable Scope

In Go, we can declare variables in two different scopes: local scope and global scope.

A variable scope specifies the region where we can access a variable. For example,

func addNumbers() {
  sum := 5 + 4
}

Here, the sum variable is created inside the function, so it can only be accessed within it (local scope). This type of variable is called a local variable.

Based on the scope, we can classify Go variables into two types:

  • Local Variables
  • Global Variables

Go Local Variables

When we declare variables inside a function, these variables will have a local scope (within the function). We cannot access them outside the function.

These types of variables are called local variables. For example,

// Program to illustrate local variables

package main
import "fmt"

func addNumbers() {

// local variables var sum int
sum = 5 + 9 } func main() { addNumbers()
// cannot access sum out of its local scope fmt.Println("Sum is", sum)
}

Output

undefined: sum

Here, the sum variable is local to the addNumbers() function, so it can only be accessed within the function.

That's why we get an error when we try to access the function from main().

To fix this issue, we can either return the value of the local variable and assign it to another variable inside the main function. Or we can make the variable sum global.


Global Variables in Golang

When we declare variables before the main() function, these variables will have global scope. We can access them from any part of the program.

These types of variables are called global variables. For example,

// Program to illustrate global variable

package main
import "fmt"

// declare global variable before main function var sum int
func addNumbers () { // local variable sum = 9 + 5 } func main() { addNumbers()
// can access sum fmt.Println("Sum is", sum)
}

Output

Sum is 14

This time we can access the sum variable from inside the main() function. This is because we have created the sum variable as the global variable.

// outside the function
var sum int

Now, the sum will be accessible from any scope (region) of the program.


Frequently Asked Questions

What are the default values of local variables and global variables?

When we declare local and global variables without assigning any values to them, they are assigned to their default values.

In Go, the default values of int and float32 types are 0.

// Program to illustrate the default values of global and local variables

package main
import "fmt"

// global variable
var variable1 int

func defaultValues() {

  // local variable
  var variable2 float32

  fmt.Println(variable1)  // 0
  fmt.Println(variable2)  // 0

}

func main() {
  defaultValues()
}

In the above example, we have initialized local and global variables. Regardless of where it's initialized, the default values of both int and float32 are 0.

What happens if both local and global variables have the same name?

If we have local and global variables with the same variable names, the compiler gives priority to the local variable. For example,

// Program to illustrate the priorities of variables

package main
import "fmt"

// define global variable var random = "Global"
func main() {
// define local variable with same name var random = "Local"
fmt.Println(random) }

Output

Local

Here, both the local and global variables have common name random. When we print random, it prints "Local", not "Global". That means the compiler is giving priority to the local variable over the global variable.

Why is local variables preferred over global variables?

In some scenarios, the use of global variables is considered bad practice. It is because a global variable can be accessed from any part of the program.

So, when one function changes the value of a global variable, other functions of the same program might not know about the changed value. This way it might create inconsistency in the value of global variables.

Let's see an example.

// Program to find the temperature

package main
import "fmt"

// global variable

var temperature float32 = 35

func findTemp1() {
  temperature = 43
}

func findTemp2() {
  temperature = 29
}

func main() {
  fmt.Println("Initial Value:", temperature)
  findTemp1()
  fmt.Println("Value after findTemp1():", temperature)
  findTemp2()
  fmt.Println("Value after findTemp2():", temperature)
}

Output

Initial Value: 35
Value after findTemp1(): 43
Value after findTemp2(): 29

Here, we have created 2 functions to find the temperature. When the findTemp2() function changes the value of temperature, findTemp1 might not know about the change. So, findTemp1() assumes the value is the same as it had defined and keeps functioning according to that value.

This can result in unexpected output and creates high confusion in larger programs.

Did you find this article helpful?