Important Characteristics of Go: Variables

Let’s learn about variable declaration, printing, and usage.

Defining and using variables

Imagine that we wanted to perform some basic mathematical calculations with Go. In that case, we need to define variables to keep our input and our results.

Go provides multiple ways to declare new variables in order to make the variable declaration process more natural and convenient. We can declare a new variable using the var keyword followed by the variable name, followed by the desired data type (we will cover data types in detail in later lessons). If we want, we can follow that declaration with = and an initial value for our variable. If there is an initial value given, we can omit the data type, and the compiler will guess it for us.

This brings us to a very important Go rule:

Note: If no initial value is given to a variable, the Go compiler will automatically initialize that variable to the zero value of its data type.

Get hands-on with 1200+ tech skills courses.