Skip to content

Variables and types in Go

New Courses Coming Soon

Join the waiting lists

In Go we define variables using var:

var age = 20

Variables can be defined at the package level:

package main

import "fmt"

var age = 20

func main() {
	fmt.Println("Hello, World!")

or inside a function:

package main

import "fmt"

func main() {
	var age = 20

	fmt.Println("Hello, World!")

Defined at the package level, a variable is visible across all the files that compose the package. A package can be composed by multiple files, you just need to create another file and use the same package name at the top.

Defined at the function level, a variable is visible only within that function. It’s initialized when the function is called, and destroyed when the function ends the execution.

In the example we used:

var age = 20

we assign the value 20 to age.

This makes Go determine the type of the variable age is int.

We’ll see more about types later, but you should know there are many different ones, starting with int, string, bool.

We can also declare a variable without an existing value, but in this case we must set the type like this:

var age int
var name string
var done bool

When you know the value you typically use the short variable declaration with the := operator:

age := 10
name := "Roger"

For the name of the variable you can use letters, digits and the underscore _ as long as the name starts with a character or _.

Names are case sensitive.

If the name is long, it’s common to use camelCase, so to indicate the name of the car we use carName

You can assign a new value to a variable with the assignment operator =

var age int
age = 10
age = 11

If you have a variable that never changes during the program you can declare it as a constant using const:

const age = 10

You can declare multiple variables on a single line:

var age, name

and initialize them too:

var age, name = 10, "Roger"


age, name := 10, "Roger"

Declared variables that are not used in the program raise an error and the program does not compile.

You will see a warning in VS Code:

Screen Shot 2022-07-28 at 15.45.31.png

and the error from the compiler:

Screen Shot 2022-07-28 at 15.45.44.png

If you declare a variable without initializing it to a value, it is assigned a value automatically that depends on the type, for example an integer is 0 and a string is an empty string.

Go is a typed language.

We saw how you can declare a variable specifying its type:

var age int

Or letting Go infer the type from the initial value assigned:

var age = 10

The basic types in Go are:

We have a lot of different types to represent intergers, you will use int most of the time, and you might choose a more specialized one for optimization (not something you need to think about when you are just learning).

An int type will default to be 64 bits when used on a 64 bits system, 32 bits on a 32 bits system, and so on.

uint is an int that’s unsigned, and you can use this to double the amount of values you can store if you know the number is not going to be negative.

All the above basic types are value types, which means they are passed by value to functions when passed as parameters, or when returned from functions.

→ Get my Go Handbook

Here is how can I help you: