Go Data Types — Zero Values
This article is part of the Data Types in Go series.
In Go, creating a variable can be done in 2 parts.
Variable Declaration: You specify the variable’s name and the type of data it’s allowed to store.
Variable Initialization: You assign an initial value to the variable, hence the name “initialization”.
Variable Initialization is actually optional and can be left out. That means that the following is perfectly valid:
var greetings string
Go still initializes the variable behind the scenes when this happens, and it does so using a default initial value, aka Zero Value. First, as a reminder here’s how to declare and initialise a variable:
Here we’re explicitly initializing the variable with an initial value. Now let’s omit the variable initialization.
This time we just printed out an empty line, that’s because the zero value for a string data type is an empty string, ""
.
Each data type has it’s own zero value: