Lesson 2 of 38 · Fundamentals
Go Variables: var, := and Multiple Assignment
Every game has to remember things: the hero's name, the current level, how much health is left. Programs keep such values in variables — named storage whose contents can change while the program runs.
Go gives you two ways to create a variable: the var keyword and the short := form. In this lesson you will learn when to use each, how to handle several variables at once, and why Go refuses to compile a variable you never use.
Declaring variables with var
The full form names the variable, its type and its starting value:
var heroName string = "Gopher"
var level int = 1
string is the type for text and int for whole numbers — the next lesson covers all of Go's basic types. When you provide a value, Go infers the type from it, so you can leave the type out:
var health = 100 // health is an int
You can also declare a variable without a value. It then starts at the zero value of its type — 0 for numbers, "" for strings:
var gold int // 0
Short variable declarations with :=
Inside a function, := declares a variable and gives it a value in one step. The type is always inferred:
package main
import "fmt"
var realm = "The Gopher Isles" // package level: only var works here
func main() {
hero := "Gopher"
level := 3
fmt.Println(hero, "explores", realm, "at level", level)
}
Gopher explores The Gopher Isles at level 3
Go programmers use := most of the time. Use var at package level (outside any function), where := is not allowed, and when a variable should start at its zero value.
Several variables at once
Both forms accept several names separated by commas; the values are assigned in order. Related variables can also be grouped in a var block:
var x, y int = 10, 20
name, level := "Gopher", 7
var (
maxHealth = 100
startGold = 50
)
Reassignment vs redeclaration
:= creates a variable; = changes one that already exists:
health := 100
health = health - 30 // reassignment: health is now 70
health := 50 // compile error: no new variables on left side of :=
With several names on the left, := is allowed as long as at least one of them is new: health, mana := 80, 40 declares mana and simply assigns health. You will see this pattern a lot once functions start returning errors.
A variable's type never changes: after health := 100, the assignment health = "full" fails with cannot use "full" (untyped string constant) as int value in assignment.
Swapping values
Go evaluates everything on the right-hand side before assigning anything, so swapping two variables needs no temporary variable:
package main
import "fmt"
func main() {
left, right := "sword", "shield"
left, right = right, left
fmt.Println(left, right)
}
shield sword
Naming variables
Go uses camelCase: heroName, maxHealth, goldPerKill. Names are case-sensitive, so level and Level are different variables. The first letter matters: a name starting with a capital letter is exported, meaning other packages can use it. That is why you call fmt.Println, not fmt.println. Local variables start with a lowercase letter. Short names like i or n suit values used for a few lines; longer-lived values deserve descriptive names.
Declared and not used
This program does not compile:
package main
func main() {
score := 10
}
./main.go:4:2: declared and not used: score
An unused local variable is usually a bug — a typo, or a result you computed and forgot to use — so Go treats it as an error, not a warning. Use the variable or delete it.
Common mistakes
:=outside a function:level := 1at package level fails withsyntax error: non-declaration statement outside function body. Writevar level = 1.:=on an existing variable in the same block: use=to change its value.=on a variable that doesn't exist yet:mana = 10fails withundefined: mana. Declare it first.- Leftover variables from experiments: Go won't compile until you use or remove them.
Assignment
The hero is ready to set out. Write a program that prints exactly:
Hero: Gopher
Level: 2
Health: 75
Left hand: shield
Right hand: sword
Build the output from variables instead of typing the values into the text:
- Declare
heroName("Gopher") withvarand the typestring. - Declare
level(1) andhealth(100) together in one:=statement. - The hero levels up and takes a hit: add 1 to
leveland subtract 25 fromhealthusing=. - Declare
leftHand("sword") andrightHand("shield") in one statement, then swap them with a single assignment. - Print each line with
fmt.Println, passing the label and the variable.Printlnputs a space between its arguments, so a label like"Hero:"needs no trailing space.
Variables and data types in other languages
- JavaScript Variables: let, const and Data Types · JavaScript
- Python Variables and Data Types · Python
- Java Variables and Primitive Data Types · Java
- C# Variables and Data Types · C#
Need a quick reminder? The Go cheat sheet has every topic of Learn Go on one page.
Lesson last updated on .