Go - Operators (Overview)
Beginner 10/10
Teacher 10/10
Architect 10/10
Try it: Given
a:=10
, b:=3
, print sum, difference, product, quotient, remainder.Arithmetic
a, b := 10, 3
_ = a + b
_ = a - b
_ = a * b
_ = a / b
_ = a % b
Comparison & Logical
_ = a == b
_ = a != b
_ = a < b
_ = (a < b) && (b > 0)
_ = (a < b) || (b == 0)
Assignment & Bitwise
a += 2; a -= 2
x := 1 << 3 // 8
Common errors
- Integer division rounding down:
7/2 == 3
. Usefloat64
for precise division.
Practice
- Compute
a/b
as float usingfloat64(a)/float64(b)
.
Quick quiz
- What does
==
compare for strings?