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:
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:
- The first
0is the value ofn. - The second
0is the value off: the zero value offloat64prints with no dot. - The two spaces in a row are the empty string
s:fmt.Printlnput a space before it and after it. falseis the zero value ofok.
Values by type
| Type | Zero value |
|---|---|
int, float64 | 0 |
string | "", the empty string |
bool | false |
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
varand no value outside functions, abovefunc 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:
var total int
total = total + 5
total = total + 7
fmt.Println(total) // 12Let's walk through the example line by line:
var total int:totalstarts at0.total = total + 5: nowtotalholds5.total = total + 7: now it holds12.fmt.Println(total)prints12.
Output with Printf
The empty string is invisible in the output of Println. fmt.Printf with suitable format signs shows each value explicitly:
var n int
var s string
var ok bool
fmt.Printf("%q %d %t\n", s, n, ok) // "" 0 falseEach format sign prints values of its own type:
%qprints a string in double quotes, so the empty string shows up as"", and a string of one space as" ";%dprints an integer;%tprintstrueorfalse.
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:
var s string
fmt.Println(s == nil) // ✗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
varand 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 aboolfalse; a string is nevernil. %dprints an integer,%qa string in quotes,%ttrueorfalse, 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.