This tutorial will explain how to work with variables in Golang, and some conventions used for idiomatic Go code.

banner

Variables in Go, just like in other languages, are used to store and represent data. Every variable in Go has a type and a value.

If you just want to read some example code, you can view it on Github.

Declaration

Lets look at the declaration of an integer type:

var i int = 5

This declares a variable named i of type int with a value of 5. The Go compiler is pretty smart, so you can sometimes omit some of the declarations. For example:

var i = 5

The compiler infers that 5 is of type int, and so assigns that type to i.

Default Values

We can also declare the type without declaring the value. If we do this, the variable is assigned a default value, which depends on the type:

var i int

Now the value of i is initialized to the default value, which is 0 for the int type.

Different types have different default values:

typeDefault ValueNotes
int0Initialized to 0
float640.0Initialized to 0
string""Empty string
*intnilPointers to any type default to nil
struct{a int}{a: 0}structs initialize to the default values of their attributes
func()nilFunction variables are initialized as nil

Shorthand Declaration

The := operator acts as a shorthand for declaration, type inference, and assignment:

i := 5
// This is equivalent to
// var i int = 5

Note that the := operator performs declaration as well as assignment, so you can’t use it on an already declared variable:

// this code will not compile
var i int
i := 5

The above code will fail, because the := operator attempts to initalize i once again, after it has already been initialized in the first line.

We can use the = operator to assign a value to variable that’s already initialized. This can be done multiple times:

// This is valid code
var i int
i = 5
i = 0
i = 4

It’s important to note the difference between the := and the = operators. The := operator is the combined declaration of a variable as well as an assignment of a value. The = operator on the other hand only assigns a value to an existing variable.

Multiple Variable Assignment

We can assign values, and even declare multiple variables at the same time:

a, b := 1, 2
// We have declared and initialized the variables `a` and `b` with the values of 1 and 2 respectively

// We can even use this to assign values later
var c int
var d int
c, d = 5, 3

Public vs Private Variables

Go has a rather special way to differentiate between public and private variables: it uses the casing of the first letter of a variable. A variable which starts with an uppercase letter is considered public, and can be accessed by other packages. If the variable begins with a lowercase letter, then it is considered private, and can only be accessed within the package in which it is declared.

package lorem

// These are all private variables, and cannot be accessed outside of package lorem
var i int
count := 5

// These are public variables
var Name string
Age := 24
package ipsum

import "lorem"

// These would cause the compiler to throw an error
fmt.Println(lorem.i)
fmt.Println(lorem.count)

// These are valid statements
fmt.Println(lorem.Name)
fmt.Println(lorem.Age)

Variable Declaration Blocks

If we want to declare multiple variables together we can make use of a variable declaration block:

var (
  // we can declare variables together in a single `var` block
  name = "John Doe"
  age = 30
  // we can assign multiple values at once
  isLorem, isIpsum = false, true
  // we can declare variables without assigning values
  info string
)

Using a block makes sense when you want to declare a group of related variables. This way, their declarations are grouped together and makes the code more readable.

Further Reading

So far, we have covered most of the basics of variables in Go. This should be enough to get you started writing your own Go applications, but if you want to see more details on how variables work, you can see the official language specification.

You can also view the working source for all the examples here on Github.