Operators in Go
Explore arithmetic, comparison, logical, bitwise, and assignment operators in Go with practical Docker-ready examples
Operators are the verbs of a programming language — the symbols that combine values and variables into expressions. Go’s operator set is deliberately small and conventional, inheriting most of its notation from C while trimming the more error-prone corners.
As a statically typed language with strong typing, Go enforces operand types strictly. You cannot add an int to a float64 without explicit conversion, and there is no implicit truthiness — only the bool type is valid in conditions. This tutorial walks through every operator category Go supports, including a few that surprise newcomers (such as &^ and the statement-form ++).
By the end you will be able to read and write Go expressions confidently, know which operators are statements (not expressions), and understand how Go’s type rules shape arithmetic.
Arithmetic Operators
Go provides the standard set: +, -, *, /, and %. Integer division truncates toward zero, while floating-point division behaves as expected. There is no exponent operator — use math.Pow for that.
Comparison Operators
The six comparison operators (==, !=, <, >, <=, >=) all yield a bool. Two values must be of comparable types; mixing an int and an int64 is a compile-time error without conversion.
Logical Operators
&&, ||, and ! are short-circuit logical operators that only operate on bool values. Unlike Python or JavaScript, Go will not let you write if x where x is an integer.
Bitwise Operators
Go offers the usual &, |, ^, <<, >>, plus a rare one: &^ (AND NOT, also called bit clear). a &^ b zeroes every bit in a that is set in b.
Assignment and Increment
Each binary arithmetic and bitwise operator has a compound assignment form (+=, *=, &^=, etc.). Go also has ++ and -- — but unlike C, they are statements, not expressions. You cannot write b := a++.
Comprehensive Example
Create a file named operators.go:
| |
Operator Precedence
Go has only five precedence levels for binary operators — far fewer than C. From highest to lowest:
* / % << >> & &^+ - | ^== != < <= > >=&&||
Unary operators (!, -, ^, *, &) bind tightest. When in doubt, parenthesize.
Create a file named precedence.go:
| |
Running with Docker
| |
Expected Output
Output of operators.go:
Arithmetic:
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
x / y = 3.4
Comparison:
a == b: false
a != b: true
a > b: true
a <= b: false
Logical:
t && f: false
t || f: true
!t: false
Bitwise:
p & q = 8
p | q = 14
p ^ q = 6
p &^ q = 4
p << 2 = 48
p >> 1 = 6
Assignment:
n += 5 -> 15
n *= 2 -> 30
n %= 7 -> 2
c after c++ twice: 2
String:
Hello, Go!
Output of precedence.go:
14
true
3
true
Key Concepts
- No implicit conversions —
int + float64is a compile error. Convert explicitly withfloat64(x)orint(y). - Integer division truncates —
17 / 5is3, not3.4. Use floating-point operands to get a fractional result. boolis its own type — conditions must evaluate tobool. There is no truthiness for ints, strings, or pointers.&^is unique — the bit-clear operator zeroes bits in the left operand wherever the right operand has bits set; useful for clearing flags.++and--are statements — they cannot appear inside expressions or assignments.b := a++does not compile.+doubles as string concatenation — but only between two strings; mixing a string and an integer is a compile error.- Precedence is flatter than C — only five binary levels. Notably,
&shares precedence with*, so1 + 2 & 3may not parse the way you expect. - No ternary operator — Go intentionally omits
?:; use a regularifstatement instead.
Comments
Loading comments...
Leave a Comment