Learn Go Variables — A Visual Guide

Easily understand Go variables with visual examples.

Inanc Gumus
Learn Go Programming
5 min readOct 4, 2017

--

Beauty of Go declarations ❤️

Go defines the variables as they read. Go is explicit and simple.

Instead of doing the arithmetic of Clockwise/Spiral Rule to read a C declaration, Go uses a simpler approach for us, humans.

→ C’s version:

Clockwise/Spiral

→ Go’s version:

Humanized

What is a variable in Go?

Type → What kind of information can be stored inside the variable.

Value → The stored value inside the variable.

Address → Where the variable can be found in computer memory.

Why do we need variables?

Without variables, there would be only static-data in our programs that we can’t change. Variables let us do dynamic things, such as getting user feedback and re-using the same value again.

How to declare a Go variable?

It’s better to talk about zero-values first. If you declare a variable without a value, then it’ll have a zero-value depending on the type of the variable.

The left side gets right side’s values if not initialized.

Long Declaration

Declares a variable named ageOfUniverse with a type of int.

The name can be anything including Unicode like π, ∑, ∫, but not this: 😱. Its value is a zero-value of an int which is 0.

Short Declaration

Declares a variable named ageOfUniverse and stores a value of 14 billion in the variable and guesses its type automatically (type-inferring).

The variable is declared and its value and type are assigned all together with := operator. When you use the short declaration, Go will not use a zero-value assignment, obviously. 👋 Pascal programmers may remember :=.

👉 For more info, you may read the Short Declarations Rulebook.

Multiple declarations

Declares multiple variables which have different types and values in the same statement.

The number of items on the left-hand side (which is: livablePlanets and ageOfEarth) should match to the number of items on the right-hand side (which is: 1 and 4.5e9).

Multiple short declaration

Declares two variables of type float64 and string, and assigns 14 billion to the first and “go” to the second variable.

Assigning values

When you want to assign a wrong type of data to a variable, Go compiler will fail to compile.

👉 Quick Tip: Swapping variables easily:

foo, bar := 1, 2
foo, bar = bar, foo
// foo will be 2, bar will be 1

💡 Which declaration form should I use?

Use a long declaration when you can’t know what data to store beforehand, otherwise, use a short declaration. Use the multiple declarations when you want to define multiple variables together or as a hint for code readability that the variables will be used together.

You can’t use short declarations outside of functions including the main function. Or: you will meet with this error: “syntax error: non-declaration statement outside function body”. Not good.

How are variables stored?

When we declare an integer variable, 4-bytes of storage space in the memory for the variable is reserved. When the data doesn’t fit into the variable, Go compiler will terminate.

However, int is a floppy type, its capacity can change. So, we can’t just simply store the age of the universe in an int variable. We should have used uint64 instead. An int can’t store ~14 billion of years, it can only store up to ~4 billion of years on 32-bit machines. Or it will overflow, but for a 64-bit machine, compilation will work.

You can’t store 14 billion in a 4-bytes storage.

Try it yourself.

Play with it: Example shows you how many bytes the ageOfUniverse variable can store.
Incorrect code. Run it to see yourself.
Correct-ish code. Run it.

Making a variable accessible to other packages

This is called exporting. To define a variable in the package scope, you can only use a long declaration or a multiple declaration. After that, you need to capitalize its first letter.

Alright, that’s all for now. Thank you for reading so far.

Let’s stay in touch:

--

--