Variables in Go

Sher Chowdhury
5 min readJul 25, 2020
Photo by James Harrison on Unsplash

Like most modern programming languages, Go uses variables for working with data. So when you create a variable, the underlying data for that variable gets saved to system memory. You can then use the variable to access, update, or delete that data.

The var statement

There are several ways to create variables, one of them is by using the var statement.

https://play.golang.org/p/FkYPusAJqDq

Let’s take a closer look at line 10. This var statement is made up of 2 parts:

Variable Declaration: This does the following:

  1. var ring-fenced some memory resource for storing the data. This memory resource is assigned with its own unique memory address. A memory address looks a bit like this, “0xc000010200”
  2. var applied a restriction on the type of data (aka data type) this memory resource is allowed to store. In this example, our memory resource is only supposed to store integers.
  3. var added an entry to the Operating System’s internal memory lookup table, linking the variable’s name, which in this example is “greetings”, to its memory address. It’s a bit like how you save a person’s name along with their phone number on your smartphone.

Variable Initialization: The assignment operator, =, assigns an initial value to this variable. This value is saved to the ring-fenced memory resource.

Let’s now take a look at the other lines:

Line 12: We used the variable’s name to access its value and printed it out.

Line 13: We used the reflect package’s TypeOf function to print out the variable’s data type.

Initialize variables with default values

You can actually declare a variable without initialization. In that scenario, the variable does get initialized, but with a default zero value.

Type Inference

So far, we’ve seen that you need 2 lines of code to create a variable:

var luckyNumber int        // Declare step
luckyNumber = 7 // Assignment step

However var can also do the assignment step as well:

var luckyNumber int = 7

Which means you can create a variable with just a single line of code (line 9):

https://play.golang.org/p/dv2JPRb93bQ

We can make line 9 even shorter by omitting the data type:

https://play.golang.org/p/U7Uv0SKfWVH

This still worked. var worked out for us, what the variable’s data type is, based on the data we provided. Declaring a variable with an implicit type is known as Type Inference.

Type Inference doesn’t always get it right, which is why on some occasions you need to explicitly specify the data type.

The short assignment operator, :=

So far we’ve gone from creating a variable using a long-form format:

var luckyNumber int
luckyNumber = 7

to getting var to declare+assign:

var luckyNumber int = 7

…to declaring a variable with an implicit type:

var luckyNumber = 7

So what next? Well, you can make this line even shorter by replacing var with the shorthand assignment operator,:=:

luckyNumber := 7

:= is an alternative to using var when declaring a variable with an implicit type.

https://play.golang.org/p/tEhuGEcoAkn

A couple of important points:

  • You can only use := inside functions. To declare variables outside of functions, you have to go back to using var
  • You can only use := when declaring variables with type inference. If you need to specify the data type explicitly, then you need to go back to using var

Declaring multiple variables

If you want to create several variables in one go, then you can do:

var name string 
var city string
var age int
var married bool

You can declare multiple variables of the same data type in a single line. That means you can rewrite the above as:

var name, city string 
var age int
var married bool

And if you also want to assign values you can do:

var name, city string = "David", "London"
var age int = 40
var married bool = true

Also if you are ok with Go “guessing” the data type for you (i.e. type inference), then you can write the above into a single line:

var name, city, age, married = "David", "London", 40, true

and since we’re inferring types, we can rewrite the above using :=:

name, city, age, married := "David", "London", 40, true

While this is more concise, it does make it a bit harder to read.

Declaring multiple variables (using the var block)

Another way to create multiple variables, is by using a var(...) block:

var (
name string
city string
age int
married bool
)

Like before, you can group variable declarations based on data types:

var (
name, city string
age int
married bool
)

and you can assign values:

var (
name, city string = "David", "London"
age int = 40
married bool = true
)

And if you want to use type inference, then you can shorten it further by omitting data types specifications:

var (
name, city = "David", "London"
age = 40
married = true
)

Also if using type inference, you can use the := short-hand assignment operator instead of a var(...) block:

name, city := "David", "London"
age := 40
married := true

Variable scopes

You can declare variables inside a function, or outside. If outside the function then the variables are accessible by other functions in the package. If the variable name starts with a capital letter, then that variable is public and accessible from outside the package. We’ll cover more about that in a later article. We’ll cover more about this in a later post.

Summary

As you can see, there are lots,…and lots,…and lots, of ways to create variables. They all have their uses and there isn’t necessarily a right or wrong way to create a variable. I’ve only scratched the surface on Go variables, and I’m planning to write more posts in future to cover Variables in more detail. But for now, let us do a quick recap on the key points we’ve covered so far.

That means that var can do the following actions in one go:

  1. The var keyword is used to declare+initialize variables. It can also assign values to variables and type inference to determine a variable’s data type.
  2. The shorthand assignment operator, :=, is an alternative to using var for creating variables with an implicit type.
  3. You can declare multiple variables using the var(...) block.

--

--