Scope is where a variable can be used. A variable can often be used inside a function, a local variable.

Sometimes a variable can be used everywhere in the program, a global variable.

There are also cases in which a variable only exists inside an statement or loop. These are also local variables.

Introduction

Variables in Go languages can be declared in three places:

  • A variable defined in the function is called a local variable. Their scope is only in the function body, that means they only exist within their function.

  • A variable defined outside the local scope is called a global variable. Global variables can be used throughout the package or even the external package (after export).

  • A variable in the function definition is called parameter

Local vs global variables

The variable x below is a local variable, it can only be used inside the main() function.


package main

import "fmt"

func main() {
    x := 7
    fmt.Println(x)
}

This code outputs:

~ go run example.go
7

If you move the variable x outside of the function, it becomes a global variable.

Global variables can be used by multiple functions. In this example example() and main()


package main

import "fmt"

var x = 7

func example() {
    fmt.Println(x)
}

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

This code outputs:

~ go run example.go
7
7
~ 

Global variables are sometimes considered a bad practice. When possible, you should pass variables as function parameters instead.

Note: The difference between local variables and global variables is where they can be used. Where they can be used is called scope. For a local variable that is often between the begin { and end } of a function.

Local variables

The variables declared by the local variable in the function body are called local variables, and their scope is only in the body of the function, and the parameters and return value variables are also local variables.

The main() function in the following instance uses local variables a, b, c:

package main

import "fmt"

func main() {
   /* local variables */
   var a, b, c int 

   /* initialize variables */
   a = 10
   b = 20
   c = a + b

   fmt.Printf ("variables: a = %d, b = %d and c = %d\n", a, b, c)
}

The output of the program is:

~ go run example.go
variables: a = 10, b = 20 and c = 30

Global variables

Variables declared outside the function are called global variables, and global variables can be used throughout the package or even external packages (after being exported).

Global variables can be used in any function, and the following example demonstrates how to use global variables:

package main

import "fmt"

/* define global variable */
var g int

func main() {

   /* define local variables */
   var a, b int

   /* initialize variables */
   a = 10
   b = 20
   g = a + b

   fmt.Printf("Values: a = %d, b = %d and g = %d\n", a, b, g)
}

The output of the program is:

~ go run example.go
Values: a = 10, b = 20 and g = 30

Variables a and b are local variables, and can only used within the main function (mains scope). Variable g is a global variable and can be used in any golang function

golang variable scope

Variable priority

The global variable in the Go language program can be the same as the local variable name, but local variables within the function are prioritized.Examples are:

package main

import "fmt"

/* declare global variable */
var g int = 20

func main() {
   /* Declare local variable */
   var g int = 10

   fmt.Printf ("value: g = %d\n",  g)
}

The output of the program is:

value:  g = 10

Parameters

Function Parameters (formal arguments) are used as local variables of the function. Here are some examples:

package main

import "fmt"

/* Declare global variables */
var a int = 20;

func main() {
   /* main() local variables */
   var a int = 10
   var b int = 20
   var c int = 0

   fmt.Printf("main() a = %d\n",  a);
   c = sum(a,b);
   fmt.Printf("main() c = %d\n",  c);
}

/* Function definition - two numbers added */
func sum(a, b int) int {
   fmt.Printf("sum() in the function a = %d\n",  a);
   fmt.Printf("sum() in the function b = %d\n",  b);
   return a + b;
}

The output of the above code execution is:

main() a = 10
sum() in the function a = 10
sum() in the function b = 20
main() c = 30

Exercises

  • What’s the difference between a local and global variable?
  • How can you make a global variable?

Download Answers