Photo by Florian Olivo on Unsplash

Go Variables — Roadmap

Jonathan Reeves
3 min readJun 9, 2023

--

In this article we will be continuing our journey to learning Go and will be covering variables and how Go handles them.

Understanding the Basics

In programming, variables are essential for storing and manipulating data in your programs. Go is no exception, and it provides a robust and flexible mechanism for declaring and using variables. In this article, we’ll take a closer look at variables in Go and understand the basic syntax for declaring and using them.

Declaring Variables

In Go, variables are declared using the var keyword, followed by the variable name and its type. If you read the Basic Syntax article I posted previously this should look familiar to you.

var age int

This declares a variable age of type int. Variables can also be initialized with a value when they’re declared:

var age int = 30

Shorthand Declaration

Go also provides a shorthand for declaring variables, which is more concise and easier to read. This is also the more commonly used approach for declaring variables that I have witnessed in large code bases. The shorthand syntax uses the ``:=` operator to declare and initialize variables in a single line. For example:

age := 34

As you can see we don’t use the var keyword here but instead we make use of the := operator to both declare and initialize the variable. There is however a caveat to using this approach. This can only be used inside of a function block. For example:

package main

import "fmt"

func main() {
age := 34
name := "Bowser"
fmt.Println("My name is", name, "I am", age, "years old)
}

This will work. I have declared both the age variable which is set to 34, an int type, as well as the name variable which is of type string. I then use the fmt.Println function to print a message to the terminal. One other thing you can see from this example is that we don’t have to use the concatenation operator, + , that is used in other languages to combine variables and strings together to form a message. Just separate out the values using a , and you are good to go.

An incorrect usage of the shorthand declaration would be the following example:

package main

import "fmt"

age := 34
name := "Bowser"

func main() {
fmt.Println("My name is", name, "I am", age, "years old.")
}

The above code would break. If you aren’t declaring variables within a block scope or a function scope, then you need to use the var keyword. So the above correctly written would be:

package main

import "fmt"

var age int = 34
var name string = "Bowser"

func main() {
fmt.Println("My name is", name, "I am", age, "years old.")
}

Type Inference

Another difference between using var and := is that the shorthand declaration doesn’t require you to explicitly declare the type. The shorthand form uses what is commonly referred to as type inference. This means that the compiler is left to infer the data type based on what value you set. For instance:

age := 30

The type of age is now set to an int as 30 is an int or integer data type. If we were to update this to:

age := 30.0

The data type would now be a float by default. This is definitely a handy feature of the language as it allows you to code in a similar style to a dynamic language like Python or JavaScript but still have the compiler backing you up if you accidentally try to replace the value of your variable with another data type later in your program.

Conclusion

Variable are an essential part of any programming language, and Go provides a flexible and easy-to-use mechanism for declaring and using them. With its simple syntax, Go makes it easy to declare variables, and its type inference means that you don’t have to explicity specify the type of a variable in many cases. Whether you’re a beginner or an experienced programmer, understanding variables is an important step in becoming proficient with Go and any other language you might learn.

--

--

Jonathan Reeves

I am a software engineer that is currently trying to break into the DevOps world using Python. Professionally I use JavaScript with React to build websites.