Go Data Types — Zero Values

Image for post
Image for post
Photo by Jonas Jacobsson on Unsplash

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:

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:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store