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
Go

2. Increment / Decrement (statements — not expressions!)

Go

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:

Go

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:

Go

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:

Go

6. Assignment & Compound Assignment

Go

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

  1. Check if a number is divisible by 3 and 5 at the same time
  2. Toggle the 4th bit (0-based) of a number using XOR
  3. Safely check if a pointer is non-nil and the value is positive
  4. 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! 🚀

You may also like...

Leave a Reply

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