Go Basics
Go Operators and Arithmetic
Why is 17 / 5 equal to 3 in Go? Learn arithmetic, comparison and logical operators.
What you'll learn
- + - * / and %
- Integer division
- &&, || and ++
Expression → result
17 / 5317 % 52float64(17) / float64(5)3.4math.Pow(2, 10)1024How Go evaluates an expression
Operators combine values into expressions. Go follows the usual math rules, with one thing to remember: dividing two integers drops the fraction.
17 / 5 is 3 because both numbers are integers, and 17 % 5 is the remainder 2. Converting both sides to float64 keeps the fraction. && is true only when both comparisons are true. count++ adds one and is a statement, not an expression.
Key points
- int / int drops the fraction: 17 / 5 is 3.
- % gives the remainder: 17
%5 is 2. - Convert to float64 to keep the fraction.
Runnable Go example
main.go
package main
import (
"fmt"
"math"
)
func main() {
a, b := 17, 5
fmt.Println(a+b, a-b, a*b)
fmt.Println(a/b, a%b)
fmt.Println(float64(a) / float64(b))
fmt.Println(a > b && b != 0)
fmt.Println(math.Pow(2, 10), math.Sqrt(144))
count := 0
count++
count += 10
fmt.Println(count)
}
22 12 85
3 2
3.4
true
1024 12
11Practice
Open the example in the online compiler, change a value or add a line, and predict the output before you run it. Then break the code on purpose and read the compiler's message.
Finished this lesson?
Mark it as learned to track your progress.
Your progress is saved in this browser Sign in to keep it on every device.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Rectangle Perimeter Compute the perimeter of a rectangle from its sides. Easy
- Simple Interest Find the total amount after simple interest. Easy