Operators

Back

Loading concept...

🎮 Golang Operators: Your Calculator Superpowers!

Imagine you have a magical calculator. This calculator doesn’t just add numbers—it can compare things, make decisions, and even talk in secret computer language! In Go (Golang), operators are the buttons on this magical calculator.


🧮 What Are Operators?

Think of operators like the action words between numbers or values.

When you write 5 + 3, the + is the operator. It tells Go: “Hey, add these two numbers together!”

Go has six families of operators. Let’s meet them all!


1️⃣ Arithmetic Operators: The Math Family

These are your basic math buttons. Just like in school!

Operator What It Does Example Result
+ Adds 5 + 3 8
- Subtracts 10 - 4 6
* Multiplies 3 * 4 12
/ Divides 15 / 3 5
% Remainder (Modulo) 17 % 5 2

🍕 Pizza Story: Understanding Modulo

You have 17 pizza slices and 5 friends. Each friend gets 3 slices (that’s 17 / 5 = 3). But wait—there are 2 slices left over! That’s 17 % 5 = 2.

Modulo tells you what’s LEFT after dividing evenly.

slices := 17
friends := 5

each := slices / friends  // 3
leftover := slices % friends  // 2

⚠️ Watch Out: Integer Division

When you divide two whole numbers in Go, you get a whole number back!

result := 7 / 2  // Result is 3, not 3.5!

Want decimals? Use decimal numbers:

result := 7.0 / 2.0  // Result is 3.5

2️⃣ Comparison Operators: The Judges

These operators compare two things and answer with true or false.

Think of them like a judge at a contest saying “Yes, that’s correct!” or “No, that’s wrong!”

Operator Meaning Example Result
== Equal to? 5 == 5 true
!= Not equal? 5 != 3 true
< Less than? 3 < 7 true
> Greater than? 10 > 2 true
<= Less or equal? 5 <= 5 true
>= Greater or equal? 8 >= 10 false

🎂 Birthday Story

yourAge := 10
rideMinAge := 8

canRide := yourAge >= rideMinAge
// canRide is true! You can ride!

🚨 Common Mistake Alert!

  • = means assign (put a value in a box)
  • == means compare (are these equal?)
x = 5   // Put 5 in box x
x == 5  // Is x equal to 5? (true)

3️⃣ Logical Operators: The Decision Makers

These operators help you combine true/false questions.

Operator Name Meaning
&& AND Both must be true
|| OR At least one must be true
! NOT Flip true to false (and vice versa)

🎢 Theme Park Story

To ride the roller coaster, you need to be tall enough AND old enough.

height := 120  // cm
age := 10

tallEnough := height >= 110
oldEnough := age >= 8

canRide := tallEnough && oldEnough
// true && true = true! You can ride!

🍦 Ice Cream Story (OR)

Mom says you can have ice cream if you finish homework OR clean your room.

homeworkDone := true
roomClean := false

getIceCream := homeworkDone || roomClean
// true || false = true! Ice cream time!

🔄 The NOT Flip

isRaining := true
canPlayOutside := !isRaining
// !true = false (can't play outside)

📊 Truth Tables Made Simple

AND (&&) - Both must say yes:

true  && true  = true
true  && false = false
false && true  = false
false && false = false

OR (||) - At least one says yes:

true  || true  = true
true  || false = true
false || true  = true
false || false = false

NOT (!) - Flip it:

!true  = false
!false = true

4️⃣ Bitwise Operators: The Secret Language

Computers think in 1s and 0s (called bits). Bitwise operators work on these tiny switches!

Think of each bit like a light switch: ON (1) or OFF (0).

Operator Name What It Does
& AND Both switches must be ON
| OR At least one switch ON
^ XOR Exactly one switch ON
<< Left Shift Move bits left (multiply by 2)
>> Right Shift Move bits right (divide by 2)

💡 Light Switch Story

Imagine two rows of light switches:

Number 5: [0][1][0][1]  (that's 5 in binary!)
Number 3: [0][0][1][1]  (that's 3 in binary!)

AND (&) - Light is ON only if BOTH switches are ON:

5 & 3:
  [0][1][0][1]
& [0][0][1][1]
= [0][0][0][1] = 1

OR (|) - Light is ON if ANY switch is ON:

5 | 3:
  [0][1][0][1]
| [0][0][1][1]
= [0][1][1][1] = 7

XOR (^) - Light is ON if switches are DIFFERENT:

5 ^ 3:
  [0][1][0][1]
^ [0][0][1][1]
= [0][1][1][0] = 6

🚀 Shift Operators: The Multiplying Magic

Left Shift (<<) - Each shift doubles the number:

x := 3      // Binary: 011
y := x << 1 // Binary: 110 = 6 (doubled!)
z := x << 2 // Binary: 1100 = 12 (doubled twice!)

Right Shift (>>) - Each shift halves the number:

x := 8      // Binary: 1000
y := x >> 1 // Binary: 0100 = 4 (halved!)
z := x >> 2 // Binary: 0010 = 2 (halved twice!)

5️⃣ Assignment Operators: The Box Fillers

Assignment operators put values into boxes (variables) and can do math at the same time!

Operator Meaning Same As
= Assign x = 5
+= Add and assign x = x + 3
-= Subtract and assign x = x - 2
*= Multiply and assign x = x * 4
/= Divide and assign x = x / 2
%= Modulo and assign x = x % 3
&= Bitwise AND assign x = x & 5
|= Bitwise OR assign x = x | 3
^= Bitwise XOR assign x = x ^ 2
<<= Left shift assign x = x << 1
>>= Right shift assign x = x >> 1

🎮 Score Story

score := 0    // Start with 0 points

score += 10   // Got 10 points! (now 10)
score += 5    // Got 5 more! (now 15)
score -= 3    // Lost 3 points (now 12)
score *= 2    // Double points bonus! (now 24)

This is shorter than writing:

score = score + 10
score = score + 5
// ... and so on

6️⃣ Operator Precedence: Who Goes First?

When you have many operators, Go follows a special order—just like math class!

🏆 The Priority List (Highest to Lowest)

graph TD A["1️⃣ Parentheses &#35;40;&#35;41;"] --> B["2️⃣ Unary: ! ++ --"] B --> C["3️⃣ Multiply/Divide: * / %"] C --> D["4️⃣ Add/Subtract: + -"] D --> E["5️⃣ Shift: &lt;&lt; &gt;&gt;"] E --> F["6️⃣ Compare: &lt; &gt; &lt;= &gt;="] F --> G["7️⃣ Equal: == !="] G --> H["8️⃣ Bitwise AND: &amp;"] H --> I["9️⃣ Bitwise XOR: ^"] I --> J["🔟 Bitwise OR: &#124;"] J --> K["1️⃣1️⃣ Logical AND: &amp;&amp;"] K --> L["1️⃣2️⃣ Logical OR: &#124;&#124;"] L --> M["1️⃣3️⃣ Assignment: = += etc"]

🧮 PEMDAS for Go!

Remember “Please Excuse My Dear Aunt Sally” from math? Go works similarly:

  1. Parentheses first ()
  2. Multiply, Divide, Modulo * / %
  3. Add, Subtract + -
  4. Then comparisons and logical operators

🎯 Examples That Make Sense

result := 2 + 3 * 4
// Go does: 3 * 4 = 12, then 2 + 12 = 14
// Result is 14, NOT 20!

Want addition first? Use parentheses!

result := (2 + 3) * 4
// Go does: 2 + 3 = 5, then 5 * 4 = 20
// Result is 20!

🎭 Complex Example

a := 5
b := 3
c := 2

result := a + b * c > 10 && a != b

Let’s break it down step by step:

  1. b * c = 3 * 2 = 6
  2. a + 6 = 5 + 6 = 11
  3. 11 > 10 = true
  4. a != b = 5 != 3 = true
  5. true && true = true

💡 Pro Tip: When in Doubt, Use Parentheses!

Even if you know the rules, parentheses make code easier to read:

// Hard to read
x := a + b * c / d - e

// Much clearer!
x := a + ((b * c) / d) - e

🌟 Quick Summary

Family Purpose Examples
Arithmetic Math operations + - * / %
Comparison Compare values == != < > <= >=
Logical Combine conditions && || !
Bitwise Work with bits & | ^ << >>
Assignment Store values = += -= *= /=

🎉 You Did It!

You now understand the six operator families in Go:

  1. Arithmetic - Your math buttons
  2. Comparison - Your judges
  3. Logical - Your decision makers
  4. Bitwise - Your secret language
  5. Assignment - Your box fillers
  6. Precedence - Who goes first!

Remember: Operators are just buttons on your magical calculator. The more you practice pressing them, the more powerful your programs become!

🚀 Next Step: Try using different operators together. Mix arithmetic with comparisons. Combine logical operators. Make your calculator dance!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.