Learn Go Operators

+ - * / work things out, % gives the remainder (17 % 5 = 2, 10 % 3 = 1), and * / % are worked out before + - (10 + 20*3 = 70). Two int values divided give an…

Bimo has 17 meatballs and 5 bowls. A meatball can't be cut, so each bowl gets 3 and 2 stay behind in the pot. The calculator on your phone says 17 divided by 5 is 3.4. Go counts exactly like Bimo: when both numbers are whole, / gives each bowl's share, % gives what is left over.

/ and % are operators, signs for working things out (+ - * / %), comparing (== != < > <= >=), and combining conditions (&& || !).

Arithmetic: + - * / %

package main

import "fmt"

func main() {
	meatballs := 17
	bowls := 5
	fmt.Println(meatballs / bowls) // 3
	fmt.Println(meatballs % bowls) // 2
	fmt.Println(10 % 3)            // 1
	fmt.Println(10 + 20*3)         // 70
	fmt.Println((10 + 20) * 3)     // 90
}
$ go run stall.go
3
2
1
70
90

Whole-number division

When both numbers are int, the result of / is an int too: the fraction is dropped, not rounded. So 17 / 5 prints 3, not 3.4. It is dropped towards zero, so -7 / 2 gives -3.

Want a decimal result? Both numbers must be float64:

fmt.Println(17.0 / 5.0)                          // 3.4
fmt.Println(float64(meatballs) / float64(bowls)) // 3.4

Watch out for zero. If bowls holds 0, meatballs / bowls stops the program at that line with panic: runtime error: integer divide by zero. The lines before it have already printed, the lines after it never run. A zero written directly, 17 / 0, is refused at compile time instead: invalid operation: division by zero. A decimal divided by zero doesn't panic: 17.0 / bowls with bowls := 0.0 gives +Inf.

Comparison and logic

A comparison gives a bool, true or false:

portions := 15
fmt.Println(portions > 10)                  // true
fmt.Println(portions == 12)                 // false
fmt.Println(portions > 10 && portions < 20) // true
fmt.Println(portions < 5 || portions > 12)  // true
fmt.Println(!(portions > 10))               // false

Assignment and ++

total := 0
total += 15 // total = total + 15
total *= 3  // total = total * 3, so 45
queue := 1
queue++ // queue becomes 2

+=, -=, *=, /= and %= are short for "work it out, then store it again". queue++ adds 1, queue-- takes 1 away. In Go, ++ is a statement, an instruction standing on its own line, not a value. x := queue++ fails with syntax error: unexpected ++ at end of statement, and the form ++queue doesn't exist at all.

Go has no power operator either. 2 ** 3 is refused, and 2 ^ 3 is not a power: it gives 1, because ^ is XOR, a bit-by-bit operation. For powers, use math.Pow(2, 3), which gives 8.

🤔 Overflow: numbers that wrap around

Every whole-number type has limits. int8 only holds -128 to 127. If the queue number is stored in an int8, the customer after number 127 gets number -128:

var queue int8 = 127
queue++
fmt.Println(queue) // -128

The number wraps round to the bottom end with no error. A value written directly past the limit is refused at compile time: var queue int8 = 128 fails with cannot use 128 (untyped int constant) as int8 value in variable declaration (overflows). A plain int on a 64-bit computer holds up to 9,223,372,036,854,775,807, so for everyday counting just use int.

⚠️ Sums that don't come out the way you expect

TL;DR: + - * / work things out, % gives the remainder (17 % 5 = 2, 10 % 3 = 1), and * / % are worked out before + - (10 + 20*3 = 70). Two int values divided give an int with the fraction dropped: 17 / 5 = 3. Need a decimal? Use float64: 17.0 / 5.0 = 3.4. % is only for whole numbers. Dividing by an int variable that is zero panics. A comparison gives a bool; && needs both sides true, || just one, ! flips it. += and its friends work it out and store it, i++ is a statement, not a value, and there is no ++i or **. An int8 wraps from 127 to -128.

⚡ Each bowl's share, and what's left

Write:

meatballs := 17
bowls := 5
fmt.Println(meatballs / bowls) // 3
fmt.Println(meatballs % bowls) // 2
Guess first: a calculator says 17 divided by 5 is 3.4. Why does Go print 3?

Two int values divided give an int too, and the fraction is dropped, not rounded. Need 3.4? Write 17.0 / 5.0.

Meaning: / gives each bowl's share, % gives what is left over, because 17 = 5 × 3 + 2.

⚡ Multiply first, then add

Write:

fmt.Println(10 + 20*3)     // 70
fmt.Println((10 + 20) * 3) // 90

Meaning: * / % are worked out before + -. Brackets change the order.

⚡ A comparison gives true or false

Write:

portions := 15
fmt.Println(portions > 10 && portions < 20) // true

Meaning: && needs both sides true, || just one, ! flips it.

⚡ ++ is not a value

Guess first: can you write x := queue++?

No. The compile fails with syntax error: unexpected ++ at end of statement.

Meaning: queue++ is a statement on its own line. The form ++queue doesn't exist in Go.

What you will learn