Variables, short declarations and zero values
A Go variable is declared with var, with a short declaration := or as a parameter. Every variable starts at the zero value of its type, and there is no uninitialised memory to read: the compiler decides where a variable lives, not the programmer.
Declarations and zero values
var i int is a declaration with a type and no value, and the value is zero: 0 for numbers, false for booleans, "" for strings, nil for pointers, slices, maps, channels and interfaces. A package-level variable is zeroed the same way before main runs.
package main
import "fmt"
var counter int
func main() {
var i int
var f float64
var b bool
var s string
var p *int
var sl []int
var m map[string]int
fmt.Printf("%T %v\n", i, i)
fmt.Printf("%T %v\n", f, f)
fmt.Printf("%T %v\n", b, b)
fmt.Printf("%T %q\n", s, s)
fmt.Printf("%T %v\n", p, p)
fmt.Printf("%T %v %v\n", sl, sl, sl == nil)
fmt.Printf("%T %v %v\n", m, m, m == nil)
fmt.Printf("package level: %d\n", counter)
}
go run zero.go
int 0
float64 0
bool false
string ""
*int <nil>
[]int [] true
map[string]int map[] true
package level: 0
%T prints the type of a value and %q quotes a string, so the empty string shows as "" rather than as nothing. sl == nil is true because a slice that was never made has no array behind it; the lesson on slices explains what that means for its length and capacity. A map is the same: nil until make.
Short declarations
Inside a function, := declares and initialises in one line, and the type comes from the value. The same statement also allows several variables at once, a swap in one line, and a mix of new and existing variables: age, city := 17, "Zurich" assigns the existing age and declares city.
package main
import "fmt"
func main() {
name, age := "Go", 16
age, city := 17, "Zurich"
a, b := 1, 2
a, b = b, a
n, err := fmt.Println("two results from Println")
fmt.Println("name:", name, "age:", age, "city:", city)
fmt.Println("swapped:", a, b)
fmt.Println("bytes written:", n, "error:", err)
}
go run short.go
two results from Println
name: Go age: 17 city: Zurich
swapped: 2 1
bytes written: 25 error: <nil>
fmt.Println returns two values, so the last line shows the second use of :=: a variable for the result a caller does not need is written _, the blank identifier, which discards it instead of leaving it unused. Go refuses an unused variable, so _ is the honest way to say "the function returns something here and I do not use it".
Scope and shadowing
A short declaration inside a block creates a new variable that hides the outer one until the block ends. That is useful for an error value per branch and dangerous when it hides a variable the code above the block expected to change.
package main
import "fmt"
func main() {
x := 1
{
x := 2
fmt.Println("inner:", x)
}
fmt.Println("outer:", x)
if x := 10; x > 5 {
fmt.Println("if scope:", x)
}
fmt.Println("after if:", x)
}
go run shadow.go
go vet shadow.go
inner: 2
outer: 1
if scope: 10
after if: 1
The inner x is a different variable: 2 inside the braces, 1 after them. The if statement has its own scope, which is why x := 10 does not touch the outer variable. go vet reports the likely mistakes of this kind, and the lesson on errors shows the pattern that uses the scope on purpose.
Where a short declaration is not allowed
At package level there is no function to declare in, so := is a syntax error. The declaration needs var, and the type can still come from the value.
package main
import "fmt"
count := 1
func main() {
fmt.Println(count)
}
go build badshort.go
# command-line-arguments
./badshort.go:5:1: syntax error: non-declaration statement outside function body
Exercises
- Declare a variable of each zero value in one program and print them with %v. Which of them print as nothing at all?
- Swap three variables in one assignment statement. How many names go on each side?
- Move the short declaration of the badshort program into main and build it again. What does the compiler say about count now?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the zero value of an int in Go?
Answer
0
Numeric types start at zero, and the variable is never uninitialised.
-
What is the zero value of a string?
Answer
the empty string
A string is a value, not a pointer, so its zero is the empty string.
-
What is the zero value of a slice?
Answer
nil
A slice variable is nil until make or a literal gives it an array; len and cap are zero.
-
What does
:=do?Answer
It declares and initialises a variable in one statement
At least one name on the left must be new; the rest are assigned.
-
What does this program print?
package main import "fmt" func main() { var b bool var i int fmt.Println(b, i) }Answer
false 0
Println writes the zero values in argument order, separated by a space.
-
What does this program print?
package main import "fmt" func main() { x := 1 { x := 2 fmt.Println(x) } fmt.Println(x) }Answer
2 / 1
The inner declaration creates a new variable that ends with its block.
-
What does this program print?
package main import "fmt" func main() { a, b := 1, 2 a, b = b, a fmt.Println(a, b) }Answer
2 1
The right side is evaluated before the assignment, so the swap works in one statement.
-
What does the compiler say about
count := 1at package level?Answer
syntax error: non-declaration statement outside function body
A short declaration is a statement, and only declarations may stand at package level.
-
What is the blank identifier for?
Answer
It discards a value the code does not use
Assigning to _ throws the value away, which is how a program avoids an unused variable.
-
Can
:=be used to change the value of an existing variable alone?Answer
No: at least one name on the left must be new
With every name already declared the statement is a compile error; plain = assigns.
The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev