Skip to content

Name and Update Values

The Course
•
18 min study

A variable is a name for a value your program needs to remember, such as the minutes spent reading or the title of a book. Inside a function, := declares a new variable and infers its type from the value. Use = to give an existing variable a new value and += to add to it. A variable keeps its type, so an int cannot later hold text. Go also rejects a local variable that is never used, so print a value while you explore it.

Two types are enough for today. An int is a whole number such as 15 or -3. A string is text written between double quotes, such as "Poems". Go works out which one you mean from the value on the right of :=.

Predict and run

What does minutes hold after +=?

Predict, then run Go
package main

import (
	"fmt"
)

func main() {
	minutes := 15
	minutes += 5
	fmt.Println(minutes)
}
Check your prediction
Text
20

Try minutes = "20". Read the type error and undo the change. Then add pages := 4 without using it and read that error too.

Choosing a name

Go names start with a letter and can contain letters, digits and underscores. The convention is short, lower-case names, with a capital letter to mark each new word: minutes, sessionCount, bookTitle. Names are case-sensitive, so minutes and Minutes are two different variables. A good name says what the value means, not what type it has: total tells a reader more than number.

Declaring with var

:= is the short form you will use most. The longer form, var, is useful when you want to state the type yourself or when there is no starting value yet. A variable declared with var and no value starts at its type's zero value: 0 for an int and the empty string "" for a string. Go never leaves a variable holding leftover memory, so you can always predict what an untouched variable contains.

var and zero values Go
package main

import (
	"fmt"
)

func main() {
	var title string = "Poems"
	var minutes int
	fmt.Println(title)
	fmt.Println(minutes)
	minutes = 25
	fmt.Println(minutes)
}
Text
Poems
0
25

Notice that the update uses =, not :=. The variable already exists, so you are changing it, not declaring it again.

Arithmetic and update operators

Whole numbers support +, -, * and /. Each one also has an update form: minutes += 5 means minutes = minutes + 5, and -=, *= and /= work the same way. The right-hand side is worked out first, using the current value, and only then stored back.

Update in place Go
package main

import (
	"fmt"
)

func main() {
	minutes := 30
	minutes -= 5
	fmt.Println(minutes)
	minutes *= 2
	fmt.Println(minutes)
	minutes = minutes / 4
	fmt.Println(minutes)
}
Text
25
50
12
Dividing whole numbers drops the remainder

50 / 4 is 12, not 12.5. When both sides are int, Go keeps only the whole part of the result. That is often what you want for counting, but keep it in mind before you divide a total to find an average. The % operator gives the remainder you lost: 50 % 4 is 2.

Three rules the compiler enforces

These rules catch real mistakes, so it helps to recognize their messages:

  1. A type stays fixed. Assigning text to an int fails with cannot use "20" (untyped string constant) as int value in assignment.
  2. Every local variable must be used. A declared but unused variable fails with declared and not used. It usually means a typo or leftover code.
  3. := must declare something new. Writing minutes := 8 when minutes already exists fails with no new variables on left side of :=. Use = to update instead.

Practice

Exercise

Your turn

Declare pages with var and no value, and print it. On two evenings you read 18 pages and then 27: add each with += and print the total. A chapter is 10 pages long. Print how many whole chapters you have finished, then how many pages you are into the next chapter. The output should be:

Text
0
45
4
5
Your working space Go
package main

func main() {
	// Write your code here.
}
Hint 1: the idea

A variable declared with var and no value starts at its zero value. For chapters and leftover pages, think of / and % on whole numbers: one keeps the whole part and the other gives the remainder.

Hint 2: a first step

Start with var pages int and print it. You should see 0 before you write the additions.

Worked solution
Run the solution Go
package main

import (
	"fmt"
)

func main() {
	var pages int
	fmt.Println(pages)
	pages += 18
	pages += 27
	fmt.Println(pages)
	fmt.Println(pages / 10)
	fmt.Println(pages % 10)
}
Text
0
45
4
5

45 / 10 is 4 because dividing two int values drops the remainder, and 45 % 10 is the 5 that was dropped. pages is declared once and then only updated, so := never appears after the var line.

Was this article helpful?

Your response is saved on this device.