Chapter 25: Operators
Operators right after slices — this is exactly the logical next step.
In Go, operators are the symbols that let you perform operations on variables and values: arithmetic, comparison, logical decisions, bitwise tricks, assignments, etc.
Go deliberately keeps the set of operators small, clean, predictable and boring (in a good way). There is:
- no operator overloading
- no custom operators
- no ternary ?: operator
- no ** power operator
- no chained comparisons like Python a < b < c
- very few surprises compared to JavaScript / Python / C++
Let’s walk through all of them group by group — exactly like a patient teacher would do it in a live coding session.
1. Arithmetic Operators
| Operator | Meaning | Works on | Example | Result | Notes |
|---|---|---|---|---|---|
| + | addition | numbers, strings | 5 + 3 “Hello” + ” ” + “World” | 8 “Hello World” | string concat with + only |
| – | subtraction | numbers | 17 – 5 | 12 | — |
| * | multiplication | numbers | 4 * 7 | 28 | — |
| / | division | numbers | 17 / 5 17.0 / 5 | 3 3.4 | integer division truncates toward zero |
| % | remainder (modulo) | integers | 17 % 5 -17 % 5 | 2 -2 | sign follows dividend |
|
0 1 2 3 4 5 6 7 8 9 10 11 |
a, b := 17, 5 fmt.Println(a / b) // 3 fmt.Println(a % b) // 2 fmt.Println(-17 / 5) // -3 (truncates toward zero) fmt.Println(-17 % 5) // -2 (sign of dividend) |
2. Increment / Decrement (statements — not expressions!)
|
0 1 2 3 4 5 6 7 8 9 10 |
i := 10 i++ // i = 11 i-- // i = 10 // j = i++ → ERROR: i++ is statement, cannot be used in expression |
Very different from C/Java/JavaScript where i++ returns the old value.
3. Comparison Operators
| Operator | Meaning | Works on | Example | Notes |
|---|---|---|---|---|
| == | equal | almost everything | a == b “hi” == “hi” | case-sensitive for strings |
| != | not equal | almost everything | a != b | — |
| < <= > >= | less / greater | numbers, strings | 3 < 5 “apple” < “banana” | string comparison = byte-by-byte lexicographical |
No chaining:
|
0 1 2 3 4 5 6 7 |
// if 10 < x < 20 { } → compile error if 10 < x && x < 20 { } // correct |
4. Logical Operators
| Operator | Meaning | Short-circuit? | Example | Notes |
|---|---|---|---|---|
| && | AND | Yes | a && b | if a is false → b not evaluated |
|
OR | Yes | ||
| ! | NOT | — | !true → false | — |
Short-circuit is very useful for safe checks:
|
0 1 2 3 4 5 6 7 8 9 |
var ptr *int if ptr != nil && *ptr > 0 { // safe — *ptr only evaluated if ptr != nil // ... } |
5. Bitwise Operators
| Operator | Meaning | Example (x=10=1010₂, y=12=1100₂) | Result (binary) | Decimal |
|---|---|---|---|---|
| & | AND | x & y | 1000 | 8 |
|
OR | x |
y | |
| ^ | XOR | x ^ y | 0110 | 6 |
| &^ | AND NOT (bit clear) | x &^ y | 0010 | 2 |
| << | left shift | x << 2 | 101000 | 40 |
| >> | right shift (arithmetic) | y >> 1 | 0110 | 6 |
&^ is Go-specific — very convenient for clearing bits:
|
0 1 2 3 4 5 6 7 |
flags := 0b1111 // 15 flags &^= 0b0010 // clear bit 1 → 0b1101 = 13 |
6. Assignment & Compound Assignment
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
x := 20 x += 5 // x = 25 x -= 3 // x = 22 x *= 2 // x = 44 x /= 4 // x = 11 x %= 3 // x = 2 x <<= 2 // x = 8 x &^= 0b01 // clear lowest bit |
No &&=, ||=, **=
7. Address / Dereference Operators (Pointers)
| Operator | Meaning | Example | Notes |
|---|---|---|---|
| & | address-of | p := &x | pointer to x |
| * | dereference | *p = 100 | change value through pointer |
8. Channel Operator (we’ll see more later)
| Operator | Meaning | Example |
|---|---|---|
| <- | receive from channel | value := <-ch |
| <- | send to channel | ch <- value |
9. Quick Practice – Try Writing These
- Check if a number is divisible by 3 and 5 at the same time
- Toggle the 4th bit (0-based) of a number using XOR
- Safely check if a pointer is non-nil and the value is positive
- Calculate 2¹⁰ using shift (without using << 10 directly — just for fun)
Which operators did you use most often?
Any part unclear right now?
- precedence when mixing &<< with +- ?
- difference between ^ (XOR) and ! (logical not)?
- why Go avoids chained comparisons?
- or ready for if / else / switch next?
Keep typing small expressions and printing results — operators are the vocabulary of logic in every programming language. You’re building a really solid base. 💪🇮🇳
See you in the next question — keep going! 🚀
