Start learning free

LESSON 4 OF 285 · WELCOME TO GO

Variables in Go

Lesson

TL ; DR:

Variables are containers for storing values.

In Go, you can declare a variable with var:

var car string = "Toyota"
var experience int = 3

Inside a function, Go also lets you use short declaration with :=:

car := "Toyota"
experience := 3


Full lesson:

A piece of information lives in memory, and we use a variable to store and label that data.

A variable declaration can include:

  • a name, such as name
  • a type, such as string or int
  • a value, such as "Michael"
var name string = "Michael"
var age int = 31

fmt.Println(name)
fmt.Println(age)

The output is:

Michael
31

Assignment
Follow the Coding Tutorial and let's create some variables.


Hint
Use var for the typed declarations and := for the short declaration step.


STEP 1 OF 3
    Ctrl + Enter to check