Introduction
Go provides a familiar set of operators for arithmetic, comparison, and logical operations, plus a full suite of bitwise operators for low-level manipulation of integer values. Unlike some languages such as C++ or Python, Go does not support operator overloading, so operators always behave according to their built-in definitions for the operand types involved.
Cricket analogy: Like a fixed set of umpiring signals (six, four, wide, no-ball) that mean exactly the same thing in every match, Go's operators behave identically for every operand type, with no team allowed to redefine what a "six" signal means.
Syntax
// arithmetic
a + b, a - b, a * b, a / b, a % b
// comparison
a == b, a != b, a < b, a <= b, a > b, a >= b
// logical
a && b, a || b, !a
// bitwise
a & b, a | b, a ^ b, a &^ b, a << n, a >> nExplanation
Arithmetic operators work as expected, with integer division truncating toward zero and % returning the remainder. Comparison operators return a bool and require both operands to be comparable types. Logical operators && (AND), || (OR), and ! (NOT) short-circuit evaluation and operate only on bool values. Bitwise operators act on integer types: & is AND, | is OR, ^ is XOR (or unary bitwise complement/NOT when used as a prefix), and the Go-specific &^ is the 'AND NOT' (bit clear) operator, which clears bits in the left operand wherever the corresponding bit in the right operand is 1. << and >> perform left and right bit shifts. Go deliberately omits operator overloading to keep operator behavior predictable and consistent across the language.
Cricket analogy: Like calculating overs as balls/6 which truncates toward zero (17 balls = 2 overs, not 2.83), while balls%6 gives the leftover deliveries, and a captain's if wicketsLeft > 0 && oversLeft > 0 short-circuits without checking overs if wickets are already zero.
Example
package main
import "fmt"
func main() {
a, b := 12, 10 // 1100, 1010 in binary
fmt.Println("a & b =", a&b) // AND
fmt.Println("a | b =", a|b) // OR
fmt.Println("a ^ b =", a^b) // XOR
fmt.Println("a &^ b =", a&^b) // AND NOT (bit clear)
fmt.Println("a << 1 =", a<<1) // left shift
fmt.Println(7%3 == 1 && 10 > 5)
}Output
a & b = 8
a | b = 14
a ^ b = 6
a &^ b = 4
a << 1 = 24
trueKey Takeaways
- Go supports arithmetic, comparison, logical, and bitwise operators, but no operator overloading.
- && and || short-circuit and only work on bool operands.
- &^ is Go's distinctive AND NOT (bit clear) operator, not found in many other languages.
- << and >> perform bitwise left and right shifts on integers.
- Integer division truncates toward zero; % gives the remainder.
Practice what you learned
1. What does the &^ operator do in Go?
2. Does Go support operator overloading for custom types?
3. What is the result of 7 / 2 using Go's integer division?
4. Which operators can short-circuit evaluation in Go?
Was this page helpful?
You May Also Like
Variables and Data Types in Go
Learn how Go declares variables with var and :=, and the built-in types like int, float64, bool, and string.
Constants and iota in Go
Understand how const declarations work in Go and how iota generates auto-incrementing enumerated constants.
Type Conversion in Go
Learn why Go requires explicit type conversion between numeric and other types, and how to perform it safely.
if-else Statements in Go
Learn how Go's if-else statement works without parentheses, including its unique init-statement syntax.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics