Go Tutorial
Estimated reading: 3 minutes 30 views

➕ Go Operators – Arithmetic, Logical, Bitwise, and More with Precedence

🧲 Introduction – Why Master Operators in Go?

Operators are the heart of expressions and logic in Go. They let you perform mathematical calculations, compare values, assign data, and even manipulate bits. Understanding operator behavior and precedence ensures you write clean, error-free, and performant Go code.

🎯 In this guide, you’ll learn:

  • The types of operators in Go and their syntax
  • Operator categories: arithmetic, assignment, relational, logical, bitwise
  • Miscellaneous operators like pointer and address-of
  • Precedence rules and how to avoid unexpected results

📘 Topics Covered

🔹 Operator Type📄 Description
Arithmetic OperatorsPerform basic mathematical operations
🧾 Assignment OperatorsAssign values and update variables
🔍 Relational OperatorsCompare two values
🔁 Logical OperatorsApply boolean logic (&&, `
⚙️ Bitwise OperatorsManipulate individual bits
🔧 Miscellaneous OperatorsUse address-of and pointer dereferencing
📊 Operator PrecedenceDefine the order in which expressions are evaluated

➗ Go – Arithmetic Operators

a := 10
b := 3
fmt.Println("Add:", a+b)
fmt.Println("Sub:", a-b)
fmt.Println("Mul:", a*b)
fmt.Println("Div:", a/b)
fmt.Println("Mod:", a%b)
OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulo

🧾 Go – Assignment Operators

x := 5
x += 3  // Same as x = x + 3
OperatorDescription
=Assign value
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign
%=Modulus and assign

🔍 Go – Relational (Comparison) Operators

a := 7
b := 10
fmt.Println(a == b)  // false
fmt.Println(a != b)  // true
fmt.Println(a > b)   // false
OperatorDescription
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

Returns a bool value (true or false).


🔁 Go – Logical Operators

a := true
b := false
fmt.Println(a && b) // false
fmt.Println(a || b) // true
fmt.Println(!a)     // false
OperatorMeaning
&&Logical AND
`
!Logical NOT

✅ Used in conditional statements like if, for, etc.


⚙️ Go – Bitwise Operators

a := 5    // 0101
b := 3    // 0011

fmt.Println(a & b)  // 1
fmt.Println(a | b)  // 7
fmt.Println(a ^ b)  // 6
fmt.Println(a << 1) // 10
fmt.Println(a >> 1) // 2
OperatorDescription
&Bitwise AND
``
^Bitwise XOR
<<Left shift
>>Right shift

🔧 Go – Miscellaneous Operators

📌 Address-of and Dereference

var a int = 42
var ptr *int = &a     // address-of
fmt.Println(*ptr)     // dereference (prints 42)
OperatorDescription
&Address-of
*Pointer dereference

📊 Go – Operator Precedence

Operator precedence determines how expressions are evaluated.

Precedence LevelOperators
1 (highest)()
2* / %
3+ -
4< <= > >= == !=
5&&
6 (lowest)`

✅ Use parentheses () to make precedence explicit and avoid confusion.


📌 Summary – Recap & Next Steps

Go provides a rich set of operators that empower you to build powerful expressions and logic. From math to memory, operators help you write precise and efficient code.

🔍 Key Takeaways:

  • Use arithmetic and relational operators to build logic
  • Bitwise operators allow low-level data manipulation
  • The & and * operators work with pointers
  • Always be mindful of operator precedence

⚙️ Real-World Use Cases:

  • Validating user input with logical conditions
  • Performing bit masking in networking
  • Formatting data output with math expressions

❓ Frequently Asked Questions

Is there a ternary operator in Go?
✅ ❌ No. Go avoids ternary (? :) to keep conditionals explicit. Use if-else instead.


Does Go support operator overloading?
✅ ❌ No. Go intentionally avoids operator overloading to keep the language simple and predictable.


Can I combine multiple operators in one line?
✅ Yes, but be cautious of precedence. Use parentheses to group logic:

if (x > 5 && y < 10) || isValid { ... }

What’s the difference between & and && in Go?
& is bitwise AND, while && is logical AND.


Can I use bitwise operations on float types?
✅ ❌ No. Bitwise operations work only on integer types (int, uint, etc.).


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Operators

Or Copy Link

CONTENTS
Scroll to Top