Go Conditions

Go (Golang) – Conditions (Decision Making)

Conditional statements in Go are used to execute different blocks of code based on true or false conditions.
Go keeps condition syntax simple and strict.


1️⃣ if Statement

Syntax


Example


 


2️⃣ if–else Statement


 


3️⃣ if–else if–else Ladder


 


4️⃣ if with Short Statement (Very Common)

Go allows a short initialization statement inside if.


x is only accessible inside the if block.


5️⃣ Logical Conditions


 


6️⃣ Nested if Statements


 


7️⃣ switch Statement (Alternative to if–else)

Basic switch


 

✔ No break needed (auto break)


8️⃣ Multiple Values in case


 


9️⃣ switch Without Expression

Works like if–else chain.


 


🔟 Type Switch

Used with interfaces.


 


Important Rules

❌ No parentheses around condition
❌ No ternary operator (?:) in Go
✔ Curly braces {} are mandatory

// ❌ Invalid
if (age > 18)
// ✅ Valid
if age > 18 {
}

Summary

  • Go uses if, if-else, and switch

  • Conditions must be boolean

  • switch is powerful and clean

  • No ternary operator

You may also like...