Go Data Types — Zero Values

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:

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:

https://play.golang.org/p/-MeCLqYNg1D

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:

https://play.golang.org/p/TZGo1lE7--w

--

--

--

Blogger at codingbee.net

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My Agile Soup

Migrate microservices applications with ease thanks to Docker Swarm

My Obsession with Simple Code

Flash Stock Rom on Xolo A1000s

11111

PWA Market Share( Progressive Web Apps) in 2021

Best Possibilities to implement Blue Green Deployment Strategy in Azure DevOps

Serverless: It’s much much more than FaaS

Using the java library

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
Sher Chowdhury

Sher Chowdhury

Blogger at codingbee.net

More from Medium

Distributed mutex based on PostgreSQL in Go. Complete example.

Create Distributed, Scalable, Durable, and Highly Available Software— With Cadence

SQL mock tests with the Goyave framework

Redis to REST, securely