Skip to content

Latest commit

 

History

History
169 lines (113 loc) · 2.18 KB

File metadata and controls

169 lines (113 loc) · 2.18 KB

➗ Arithmetic Operators in Go — Clean Breakdown

Go keeps arithmetic simple, predictable, and strict. No hidden behavior, no surprises.


🔢 Core Arithmetic Operators

➕ Addition (+)

Adds two values.

a := 10
b := 5
c := a + b // 15

Also used for string concatenation:

s := "Go" + "Lang"

➖ Subtraction (-)

Subtracts right from left.

a := 10
b := 5
c := a - b // 5

✖️ Multiplication (*)

Multiplies values.

a := 10
b := 5
c := a * b // 50

➗ Division (/)

Divides left by right.

a := 10
b := 5
c := a / b // 2

⚠️ Important behavior:

  • Integer division truncates decimals
a := 10
b := 3
c := a / b // 3 (not 3.33)

🧮 Modulus (%)

Returns remainder.

a := 10
b := 3
c := a % b // 1

⚡ Compound Assignment Operators

These combine operation + assignment.

+=

a := 10
a += 5 // 15

-=

a := 10
a -= 5 // 5

*=

a := 10
a *= 2 // 20

/=

a := 10
a /= 2 // 5

%=

a := 10
a %= 3 // 1

🧠 Type Rules (VERY IMPORTANT)

Go is strict:

var a int = 10
var b float64 = 3.5

// ❌ invalid
// c := a + b

✔ Correct way:

c := float64(a) + b

⚡ Key Behavior Summary

  • All arithmetic is type-safe
  • No automatic mixing of int, float64, etc.
  • Integer division drops decimals
  • % only works on integers
  • Strings can only use +

🧠 Mental Model

  • + → combine / grow
  • - → reduce / offset
  • * → scale
  • / → split
  • % → leftovers

🚀 Summary

Go arithmetic is intentionally minimal and strict. You get predictable math behavior without hidden conversions or runtime surprises — which is exactly why it scales well in systems and backend engineering.