×

go · level 1 · unit 3 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Go is a very fast calculator.

Go does maths with operators: + adds, - takes away, * multiplies and / divides. Brackets go first, then * and /, then + and -, like at school.

Careful with /. Divide one int by another and you get a whole number: 7 / 2 is 3, and the half is thrown away. To keep it, use a float64, like 7.0 / 2, which is 3.5.

% gives the , what is left after sharing out. Share 7 sweets between 2 friends: each gets 7 / 2, which is 3, and 7 % 2, which is 1, is left over.

package main
import "fmt"
func main() {
	sweets := 7
	friends := 2
	fmt.Println("Each friend gets", sweets/friends)
	fmt.Println("Left over:", sweets%friends)
	fmt.Println("Pizza each:", 3.0/2)
}

Go will not mix an int and a float64 in one sum, so keep your boxes the same type.