Go Variables - A Visual How-to Guide

Published Oct 11, 2017Last updated Oct 12, 2017
Go Variables - A Visual How-to Guide

Easily understand Go variables with visual examples.

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[1] to read a C declaration, Go uses a simpler approach for us humans.

→ C’s version:

C Declaration
Clockwise/Spiral Rule

→ Go’s version:

Go Declaration

What is a variable in Go?

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.

Zero Values
Left side gets right side’s values if not initialized.

Long Declaration

Long Variable Declaration

Declares a variable named ageOfUniverse with type of int.

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

Short Declaration

Short Variable Declaration

Declares a variable named ageOfUniverse and stores a value of 14 billions 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 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 declaration

Multiple Variable Declaration

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

Multiple Short Variable Declaration

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

Assigning values

Simple Assignment

Multiple Assignment

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

Wrong Assignment

💡 Which declaration form should I use?

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

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

How variables are 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 billions of years, it can only store up to ~4 billions of years on 32-bit machines. Or it will overflow, but for a 64-bit machine, compilation will work.

How Golang Variables are Stored

Try it yourself.

package main

import (

func main() {
  var ageOfUniverse int
  reservedBytes := unsafe.Sizeof(ageOfUniverse)

  fmt.Printf("Number of bytes reserved for `ageOfUniverse` variable is %d bytes.\n",

Play with it: Example shows you how many bytes the ageOfUniverse variable can store.

package main

import (

// on 32-bit machines, it'll overflow
// on 64-bit machines, everything is fine
// use: uint64 instead.
func main() {
  var ageOfUniverse int = 14e9 // 14e9 = 14 and 9 zeros which is 14 billions

  fmt.Printf("`ageOfUniverse` is %d.\n", ageOfUniverse)

Incorrect code. Run it to see yourself.

package main

import (

func main() {
 	// still not a good way to define a variable like this. we will see it.
  var ageOfUniverse uint64 = 14e9 // 14e9 = 14 and 9 zeros which is 14 billions

  fmt.Printf("`ageOfUniverse` is %d.\n", ageOfUniverse)

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 long declaration or multiple declaration. After that, you need to capitalize its first letter.

Making a variable accessible to other packages

🔥 I’m creating an online course for Go: Join to my newsletter!
Let’s stay in touch weekly for new tutorials and for my online Go course.

❤️ Please Share this post on twitter!

🐦 I’m mostly tweeting about Go: @inancgumus.

This article originally published as Learn Go variables—A visual guide in Learn Go Programming.

  1. http://c-faq.com/decl/spiral.anderson.html ↩︎

Discover and read more posts from Inanc Gumus
get started
Enjoy this post?

Leave a like and comment for Inanc