Variables

This lesson discusses how variables are used in Go.

Introduction

A value that can be changed by a program during execution is called a variable. The general form for declaring a variable uses the keyword var as:

var identifier type 

Here, identifier is the name of the variable, and type is the type of the variable. As discussed earlier in this chapter, type is written after the identifier of the variable, contrary to most older programming languages. When a variable is declared, memory in Go is initialized, which means it contains the default zero or null value depending upon its type automatically. For example, 0 for int, 0.0 for float, false for bool, empty string ("") for string, nil for pointer, zero-ed struct, and so on.

Run the following program to see how declaring a variable works.

Get hands-on with 1400+ tech skills courses.