Operators in Go
# CHAPTER 5
Operators in Go
1. Introduction
Programming is largely about manipulating data. Whether calculating the total price in a shopping cart, checking if a user's age is over 18, or combining multiple security conditions, Operators are the symbols that perform these actions. Go provides a standard, robust set of operators similar to C, Java, and Python.2. Learning Objectives
By the end of this chapter, you will be able to:- Perform math using Arithmetic Operators.
- Compare variables using Comparison Operators.
- Combine conditions using Logical Operators.
- Update variables efficiently using Assignment Operators.
- Understand the basics of Bitwise Operators.
3. Arithmetic Operators
Used for standard mathematical calculations.| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (Remainder) | 10 % 3 | 1 |
++ | Increment | x++ | Increases x by 1 |
-- | Decrement | x-- | Decreases x by 1 |
Important Note on Division: If you divide two integers, Go truncates the decimal. 5 / 2 results in 2. To get 2.5, at least one number must be a float64: 5.0 / 2.0.
4. Assignment Operators
Used to assign values to variables.| Operator | Example | Equivalent to |
|---|---|---|
= | x = 5 | Assigns 5 to x |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
5. Comparison Operators
Used to compare two values. They always return abool (true or false).
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 3 | false |
!= | Not equal | 5 != 3 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater or equal | 5 >= 5 | true |
<= | Less or equal | 5 <= 3 | false |
6. Logical Operators
Used to combine multiple comparison conditions.| Operator | Name | Description | Example |
|---|---|---|---|
&& | Logical AND | True ONLY if BOTH are true | (5 > 3) && (2 < 4) is true |
|| | Logical OR | True if AT LEAST ONE is true | (5 < 3) || (2 < 4) is true |
! | Logical NOT | Reverses the result | !(5 == 5) is false |
7. Bitwise Operators (Advanced)
These manipulate data at the raw binary level (1s and 0s). They are mostly used in cryptography, compression, or systems programming.-
&(AND)
-
|(OR)
-
^(XOR)
-
<<(Left Shift)
-
>>(Right Shift)
*Note: As a beginner backend developer, you rarely use bitwise operators. Focus on Arithmetic, Comparison, and Logical operators first!*
8. Strict Typing with Operators (Common Mistake)
In Javascript or PHP, you can add a string and a number together ("5" + 2), and the language will silently guess what you mean.
Go does not allow this. Go's strict static typing means you cannot perform arithmetic operations on mismatched types.
9. Best Practices
-
Parentheses for clarity: While
a + b * cworks because of the order of operations,a + (b * c)is instantly readable to other developers.
-
No Pre-increment: In C/C++, you can write
++xorx++. Go *only* allows postfix increments (x++), and it is a statement, not an expression. You cannot writey = x++. This design choice eliminates thousands of confusing bugs found in C.
10. Exercises
-
1.
Create a variable
score := 100. Use the+=operator to add 50 to it, then print it.
-
2.
Create two variables:
x := 10andy := 3. Print the result ofx % yand explain what the output means.
11. MCQs with Answers & Explanations
What is the result of 10 % 4 in Go?
Which operator checks if two values are exactly equal?
What does x += 5 mean?
If a = true and b = false, what is a && b?
What is the Logical OR operator?
What does !true return?
How does Go evaluate int(5) / int(2)?
What happens if you try to add an int and a float64 in Go?
++x (pre-increment) allowed in Go?
a) Yes b) No
Answer: b) No. *Explanation: Go only allows postfix x++ to keep the language syntax simple and prevent confusing expression bugs.*
Q10. Can you use x++ inside an assignment like y = x++ in Go?
a) Yes b) No
Answer: b) No. *Explanation: x++ is a statement in Go, not an expression that returns a value.*
12. Interview Preparation
Interview Questions:-
1.
Why does Go prevent operations between an
intand afloat64?
-
2.
Explain the difference between
==and=.
Common Pitfall: Forgetting that integer division drops the decimal. If you are calculating an average, always convert the integers to float64 before dividing, otherwise your average will be inaccurate.