Skip to content
Theory · Variables and types · lesson 1 of 10

Variables

What you'll learn

What you'll learn:

  • declare variables with var and := and change their values with =;
  • tell Go's basic types apart and convert a value from one type to another;
  • explain what a variable declared without a value holds;
  • declare constants and number them with iota.
A hand puts a tile 42 into a box labelled age

Picture a box with a label. The label carries a name, and the box holds a value. By the name you can find the box, look inside it or put something else there. That is how a program keeps data—in variables, named values that can be read and changed.

Before you put anything into a box, you have to get the box and label it. Go requires a variable to be declared before its first use. You cannot assign a value to a name the compiler knows nothing about yet:

GO
package main

import "fmt"

func main() {
    count = 5 // ✗
    fmt.Println(count)
}
TEXT
undefined: count

The compiler does not create a variable on its own at the first assignment. First you declare it: you give its name and the type of the value it will hold. The type is the shape of the box: a box for whole numbers takes no text.

Three forms of declaration

Go gives you three ways to declare a variable. Here are all three side by side:

GO
var a int = 1
var b = 2
c := 3
fmt.Println(a, b, c) // 1 2 3

All three variables are integers, and fmt.Println prints 1 2 3. Let's look at each form on its own.

The full form: var a int = 1

The line reads left to right:

  • var is the keyword: "I'm declaring a variable";
  • a is the variable's name;
  • int is its type;
  • = 1 is the initial value.

You write the full form when the type must not follow from the value. So var price float64 = 3 creates a fractional variable from a whole number. Without float64, the compiler would pick the type int for price.

var without a type: var b = 2

Here the type is left out. The compiler looks at the value 2 and picks the type int for b itself.

This form is shorter than the full one and works everywhere: inside functions and outside them. Variables declared above func main are usually written this way.

Short declaration: c := 3

The := operator is a short variable declaration. It declares a variable and gives it a value at once, and, like var without a type, it takes the type from the value.

Inside functions this is the most common form. But it has a limit:

  • := works only inside a function;
  • the line x := 1 above func main does not compile: syntax error: non-declaration statement outside function body;
  • outside functions, variables are declared only with var.

Variable names

A variable name starts with a letter and consists of letters and digits. Go writes compound names as one word, starting each following word with a capital letter: itemCount, maxSpeed.

Assignment

The = operator changes the value of a variable that is already declared. With := you can declare several variables at once:

GO
x, y := 1, 2
x = x + 10
fmt.Println(x, y) // 11 2

Let's go line by line:

  1. x, y := 1, 2 creates two variables: x gets 1, and y gets 2.
  2. x = x + 10 reads the old value of x, adds 10 to it and writes the result back into x.
  3. fmt.Println(x, y) prints 11 2: the box x now holds 11, and nobody touched y.

Here the box picture is off. When you take a thing out of a real box, the box is empty. Reading a variable means looking at the value, not taking it away: after fmt.Println(x), x holds the same number.

Constants

The keyword const declares a constant. Its value is known at compile time and never changes afterward—like a box sealed before the program even starts.

  • The constant const limit = 10 can be read, but it cannot be assigned a new value.
  • Constants, like variables declared with var, are allowed both inside functions and outside them.

Interviewers often ask how var, const and := differ. The answer comes from this lesson:

  • var declares a variable anywhere—inside a function or outside it;
  • := declares a variable only inside a function;
  • const declares a value known at compile time, and it cannot be changed.

Common mistake

The := operator is used for a variable that is already declared:

GO
x := 1
x := 2 // ✗
fmt.Println(x)
TEXT
no new variables on left side of :=

:= always declares at least one new variable, and x already exists here. To change the value, use the = operator: x = 2.

Key points

  • var declares a variable both inside and outside functions, while := works only inside a function.
  • = changes the value of a declared variable, while := always creates at least one new one.
  • const declares a value known at compile time; it cannot be assigned a new one.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way