Variable Scope

The scope of a variable is the part of the program where you can use it. In Go, a variable lives inside the block where you declare it. A block is the code between { and }: the body of a function, of an if, or of a for. When the block ends, the variable is gone.

package main

import "fmt"

func main() {
	score := 70
	if score >= 50 {
		message := "pass"
		fmt.Println("Inside:", message)
	}
	fmt.Println("After:", message)
}

Output:

# command-line-arguments
./main.go:11:24: undefined: message

message was declared inside the if block, so on line 11 it no longer exists. score is different. It was declared in the body of main, so it can be used anywhere in main, also inside the if. Inner blocks can see the variables of outer blocks, but not the other way around.

To use a value after the block, declare the variable before it. Inside the block, change it with =:

score := 70
message := "fail"
if score >= 50 {
	message = "pass"
}
fmt.Println(message)

Output:

pass

The same rule holds for functions. A variable declared in one function cannot be used in another one, and the i of a for loop is gone when the loop ends.

Shadowing: two variables with one name

:= makes a new variable. Inside a block, it can make a new variable with the same name as one outside. The new one hides the old one until the block ends. This is called shadowing, and it causes many bugs in Go programs:

package main

import "fmt"

func main() {
	score := 50
	bonus := true
	if bonus {
		score := score + 10
		fmt.Println("Inside:", score)
	}
	fmt.Println("After:", score)
}

Output:

Inside: 60
After: 50

score := score + 10 made a second score that lives only in the if block. The outer score never changed, so it is still 50 after the block. The program compiles with no error, so you must watch for := inside blocks yourself. The fix is one character: write score = score + 10 to change the outer variable.

Package-level variables

A variable declared with var outside every function is a package-level variable. Every function in the file can use it:

package main

import "fmt"

var visits int

func visit() {
	visits++
}

func main() {
	visit()
	visit()
	fmt.Println(visits)
}

Output:

2

Use these rarely. Any function can change them, so in a big program it is hard to find out which code changed the value. Passing values in as parameters and returning results is easier to follow.

Parameters are copies

A parameter is a new variable that belongs to the function. A call copies the argument's value into it. So changing a parameter does not change the caller's variable:

package main

import "fmt"

func double(n int) {
	n = n * 2
	fmt.Println("Inside:", n)
}

func main() {
	x := 5
	double(x)
	fmt.Println("After:", x)
}

Output:

Inside: 10
After: 5

n started as a copy of x. Doubling n did nothing to x. If the caller needs the new value, change double to return n, and then write x = double(x). The Pointers lesson shows another way.

The exercise program has two scope bugs: a variable used outside its block, and a shadowed variable. Find both and fix them.