Skip to content
Theory · Variables and types · lesson 6 of 10

Zero values

What is inside a new box that nobody has put anything into yet? In Go the answer is always known in advance. Every new box comes with default contents that depend on its shape: a box for numbers holds 0, and a box for text holds an empty string.

A variable can be declared with var and no initial value. In some languages, reading such a variable is an error or a random number from memory. In Go its value is always defined:

GO
package main

import "fmt"

func main() {
    var n int
    var f float64
    var s string
    var ok bool
    fmt.Println(n, f, s, ok) // 0 0  false
}

Every variable declared without a value gets the zero value of its type. That is why Go has no "variable not initialized" error: a variable can be read right after its declaration.

Let's take the output 0 0 false apart:

  1. The first 0 is the value of n.
  2. The second 0 is the value of f: the zero value of float64 prints with no dot.
  3. The two spaces in a row are the empty string s: fmt.Println put a space before it and after it.
  4. false is the zero value of ok.

Values by type

TypeZero value
int, float640
string"", the empty string
boolfalse

For some Go types the zero value is called nil and means "nothing". This does not apply to string: a string is never nil, and the empty string is written as "".

Here the box picture is off. An empty box in real life means "nothing". A new variable in Go is not empty: it holds a real value, such as 0, and you can compute with it, compare it and print it.

Where a zero value appears

A variable declared with var and no value gets its zero value. Here are the rules:

  • a short variable declaration without a value does not exist: a value always stands to the right of :=;
  • that is why a variable with a zero value is declared only with var;
  • a variable declared with var and no value outside functions, above func main, also gets its zero value.

A zero value as a start

A zero value works well as a starting value. A counter or a sum is declared with var, and you start adding to it right away. The line var total int = 0 says the same thing, only longer:

GO
var total int
total = total + 5
total = total + 7
fmt.Println(total) // 12

Let's walk through the example line by line:

  1. var total int: total starts at 0.
  2. total = total + 5: now total holds 5.
  3. total = total + 7: now it holds 12.
  4. fmt.Println(total) prints 12.

Output with Printf

The empty string is invisible in the output of Println. fmt.Printf with suitable format signs shows each value explicitly:

GO
var n int
var s string
var ok bool
fmt.Printf("%q %d %t\n", s, n, ok) // "" 0 false

Each format sign prints values of its own type:

  • %q prints a string in double quotes, so the empty string shows up as "", and a string of one space as " ";
  • %d prints an integer;
  • %t prints true or false.

If a sign does not match the argument's type, the program does not stop. For a string, fmt.Printf("%d\n", s) prints %!d(string=).

Zero values are a frequent interview question. Interviewers usually show a program like this one and ask what it prints. The answer takes two things:

  • the zero value of each type;
  • how each format sign prints it.

Common mistake

The empty string is compared with nil. The == operator compares two values and gives true or false:

GO
var s string
fmt.Println(s == nil) // ✗
TEXT
invalid operation: s == nil (mismatched types string and untyped nil)

A value of type string cannot be nil, and the compiler rejects the comparison. The empty string is checked by comparing it with "": s == "".

Key points

  • A variable declared with var and no value gets the zero value of its type and can be read right away.
  • The zero value of numbers is 0, of a string "", of a bool false; a string is never nil.
  • %d prints an integer, %q a string in quotes, %t true or false, so the empty string shows up as "".

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way