🧮 Go Operator Precedence – Understand Evaluation Order in Go (2025 Guide)
🧲 Introduction – What Is Operator Precedence in Go?
In Go, operator precedence determines the order in which expressions are evaluated. When an expression involves multiple operators, Go follows specific rules to decide which operation happens first—similar to the BODMAS rule in math.
🎯 In this section, you’ll learn:
- Go’s complete operator precedence table
- How associativity works with same-level operators
- Common precedence pitfalls
- Real-world examples with and without parentheses
📏 Why Operator Precedence Matters
Take the following line:
result := 10 + 5 * 2
Should Go evaluate 10 + 5
first, or 5 * 2
?
✅ Answer: It computes 5 * 2
first, because *
has higher precedence than +
.
📋 Go Operator Precedence Table
Precedence | Operators | Category | Associativity |
---|---|---|---|
1 (Highest) | * , / , % | Multiplication/Division | Left-to-right |
2 | + , - | Addition/Subtraction | Left-to-right |
3 | << , >> , & , &^ | Bitwise operations | Left-to-right |
4 | < , <= , > , >= | Comparison | Left-to-right |
5 | == , != | Equality | Left-to-right |
6 | ^ , ` | ` | Bitwise OR/XOR |
7 | && | Logical AND | Left-to-right |
8 | ` | ` | |
9 (Lowest) | = += -= *= /= %= etc. | Assignment | Right-to-left |
🧠 When operators have the same precedence, associativity decides the direction of evaluation.
🧪 Example – Precedence in Action
a := 10 + 5 * 2 // Output: 20
Evaluation steps:
5 * 2
→10
10 + 10
→20
✅ *
has higher precedence than +
👨🔬 More Examples
x := 3 + 4 * 2 > 10 && true
Evaluation order:
4 * 2
→ 83 + 8
→ 1111 > 10
→ truetrue && true
→ true
✅ Final result: true
🧠 Use Parentheses to Control Order
If you’re unsure about operator order or want to make logic clearer, always use parentheses.
x := (3 + 4) * 2 // Output: 14
Without parentheses:
3 + 4 * 2 = 3 + 8 = 11
⚠️ Common Pitfalls
Mistake | Fix using parentheses |
---|---|
Assuming + runs before * | Use (a + b) * c explicitly |
Chaining comparisons like a < b < c | Split: a < b && b < c |
Ignoring associativity in logic | Group with () as needed |
🔄 Associativity Rules
- Most operators are left-associative: evaluated left to right.
- Assignment operators (
=
,+=
, etc.) are right-associative.
Example:
x := y := 5 // ❌ Invalid in Go
Go does not support chained assignments like C or Python. Declare separately.
📌 Summary – Recap & Next Steps
Operator precedence in Go helps avoid unexpected results in complex expressions. When in doubt, use parentheses to make your code safer and easier to read.
🔍 Key Takeaways:
- Operators with higher precedence are evaluated first
- Associativity resolves tie-breaking at the same level
- Use parentheses to override precedence and clarify intent
- Avoid chaining comparisons or assignments in Go
⚙️ Next: Dive into Go Conditional Statements like if
, else
, and switch
for decision-making logic.
❓ FAQs – Go Operator Precedence
❓ Does Go follow the BODMAS rule?
✅ Yes. Go’s operator precedence is similar to BODMAS with multiplication/division before addition/subtraction.
❓ Can I chain assignments like x = y = 5
in Go?
✅ No. Go does not support chained assignments.
❓ How do I ensure an operation happens first?
✅ Use parentheses ()
to enforce your desired evaluation order.
❓ What happens if I mix +
, *
, and >
in one line?
✅ Go will compute *
first, then +
, and finally compare with >
as per precedence rules.
❓ Are logical operators higher than arithmetic ones?
✅ No. Logical operators (&&
, ||
) have lower precedence than arithmetic and comparison operators.
Share Now :