What a variable is
Every piece of information a program works with lives in memory. A variable gives one such piece a name, so you can store it, read it back and change it later. Without variables, every value would have to be written out again each time you needed it.
A Go variable has three parts: a name such as name, a type such as string or int, and a value such as "AlgoCademy". The type is fixed when the variable is declared and cannot change afterwards.
Syntax and examples
Declaring with var
The full form is the keyword var, the name, the type and the value. In Go the type comes after the name:
var name string = "AlgoCademy"
var age int = 10
fmt.Println(name)
fmt.Println(age)
AlgoCademy
10
Text values go in double quotes. Numbers have no quotes: 31 is a number, "31" is a string.
The short declaration :=
Inside a function, you can leave out both var and the type. The operator := declares the variable and lets Go infer the type from the value:
car := "Toyota"
experience := 3
fmt.Printf("%T %T\n", car, experience)
string int
The format verb %T prints a value's type. A whole number like 3 becomes an int, a number with a decimal point like 3.5 becomes a float64, and text becomes a string. The short form is what most Go code uses inside functions.
Changing a value
After a variable exists, assign a new value with plain =. The new value must have the same type:
age := 31
age = 32
fmt.Println(age)
32
Default values
A variable declared with var and no value starts at its type's zero value: 0 for numbers, "" for strings and false for booleans:
var count int
var label string
var ok bool
fmt.Printf("%d %q %t\n", count, label, ok)
0 "" false
Several variables at once
One := can create several variables. The values on the right are assigned to the names on the left, in order:
c, d := 2, 3.5
fmt.Println(c, d)
2 3.5
Here c is an int and d is a float64. If at least one name on the left is new, := is allowed, and it just assigns to the names that already exist. That rule shows up often when functions return more than one value.
Common mistakes and gotchas
- Declaring the same name twice with
:=. Afterx := 1, writingx := 2in the same scope fails withno new variables on left side of :=. Usex = 2to change it. - Using
=before declaring. A plain assignment only works on a variable that already exists. - Putting
:=outside a function. At the top level of a file,x := 5is a syntax error (non-declaration statement outside function body). Usevar x = 5there. - Wrong value for the type.
var age int = "31"fails withcannot use "31" (untyped string constant) as int value. The same happens when you assign text to anintvariable later. - Unused variables. Go refuses to compile a program with a local variable that is declared and never used (
declared and not used). Use it, for example by printing it, or remove it. - Type after the name.
int ageis wrong. Go writesage int.
The exercise
The lesson asks for three variables on three lines.
- Declare
namewithvar, typestring, value"Michael". - Declare
agewithvar, typeint, value31. It is a number, so it has no quotes. - Declare
languagewith:=and the value"Go". Go infers the typestringfrom the value, so you don't write it.
var name string = "Michael"
var age int = 31
language := "Go"
The first two lines state the type explicitly, and the third lets Go work it out. All three do the same job of creating a named value. The variables are only declared, not printed; printing them is one fmt.Println call each, as shown above.
Key takeaways
- A variable has a name, a type and a value; the type is fixed once declared.
var name type = valueis the full form, and the type comes after the name.name := valuedeclares a variable inside a function and infers its type.- Use
=to change an existing variable and:=only to create a new one. - Go rejects unused variables and values of the wrong type at compile time.