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
stringorint - 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.
Free plan: 30 tutor messages. PRO is unlimited.
