Skip to content
Justice Tech School

Scope and Shadowing

Variables live in their block. An inner := can shadow an outer variable with the same name.

Example
package main

import "fmt"

func main() {
	x := 1
	if true {
		x := 2
		fmt.Println("inner", x)
	}
	fmt.Println("outer", x)
}

Result

inner 2
outer 1

Exercise

Block scope.

Variables live in a 

Quiz Test

What does shadowing do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.