Chapter 24: Go Operators

Go Operators — one of the very first things every beginner needs to feel comfortable with, because almost every line of real code uses them.

Operators in Go are very clean and minimal compared to many other languages — no surprises like JavaScript’s == vs ===, no Python’s ** power operator, no C++ overloading mess. Go has exactly the operators you expect from a modern C-family language, with very few additions and almost no weird behaviors.

I’ll explain them like we’re sitting together with VS Code open: group by group, precedence table, examples you can run immediately, common patterns, gotchas, and when to prefer which style.

1. Quick Overview – All Operator Groups in Go

Priority (highest first) Group Operators Associativity Notes / Special in Go
1 Primary / postfix x() x.y x[y] x[y:z] x[y:z:w] left-to-right slice expressions too
2 Unary +x -x !x ^x &x *x <-x ++x –x right-to-left <- only for channels
3 Multiplicative * / % << >> & &^ left-to-right &^ = bit clear
4 Additive + – ^ left-to-right
5 Comparison == != < <= > >= No chaining like Python
6 Logical AND && left-to-right short-circuit
7 Logical OR
8 Channel send / receive <- (as binary) right-to-left rare outside goroutines
9 Assignment / compound assign = += -= *= /= %= <<= >>= &= = ^= &^= right-to-left

Important Go rules:

  • No operator overloading (unlike C++/Python)
  • No ternary ?: operator — use if-else
  • No &&= or ||= compound logical operators
  • Comparison operators do not chain (a < b < c is invalid)

2. Detailed Examples by Group (Copy-Paste & Run)

Create operators.go:

Go

3. Very Important Go-Specific Behaviors

  1. Integer division truncates toward zero 17 / 5 = 3, -17 / 5 = -3 (not -4 like some languages)

  2. No automatic promotion You must convert types explicitly for mixed operations

    Go
  3. Bit clear &^ (unique to Go) a &^ b = bits of a with bits of b turned off

    Go
  4. No ** power operator Use math.Pow or repeated multiplication for integers

  5. Logical operators short-circuit Very useful for safe pointer / nil checks

    Go

4. Your Quick Practice Exercise

Try to write expressions for these:

  1. Check if a number is even and positive
  2. Toggle the 3rd bit (0-indexed) of a number using bitwise operators
  3. Safely divide two integers and check for division by zero
  4. Compare two strings ignoring case (hint: use strings.ToLower)

Which operators did you use most?

Questions now?

  • Precedence confusion in complex expressions?
  • Bitwise tricks for flags / masks?
  • How operators work with custom types later (methods)?
  • Or ready for control structures (if, for, switch) next?

Keep typing these examples — operators are the glue that holds almost every logic together. You’re building a really solid foundation step by step. 💪🇮🇳 Let’s keep going! 🚀

You may also like...

Leave a Reply

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