Go – Decision Making
Estimated reading: 3 minutes 36 views

🔁 Go If Statement – Write Conditional Logic in Go (2025 Guide)

🧲 Introduction – Why Use If Statements in Go?

The if statement in Go allows you to execute blocks of code conditionally—based on whether a boolean expression is true or false. It’s a foundational control structure used in decisions, validation, loops, and error handling.

🎯 In this section, you’ll learn:

  • The basic syntax of if, if-else, and if-else if
  • How to use inline variable declarations
  • Best practices for writing clean conditional logic
  • Real-world usage examples

✅ Basic Syntax – Go If Statement

if condition {
    // code block executes if condition is true
}

Example:

x := 10
if x > 5 {
    fmt.Println("x is greater than 5")
}

📤 Output:

x is greater than 5

🔀 If-Else Statement

Use else to handle the case when the if condition is false.

x := 3
if x > 5 {
    fmt.Println("x is large")
} else {
    fmt.Println("x is small")
}

📤 Output:

x is small

🔁 If-Else If-Else Ladder

Check multiple conditions in sequence:

score := 85

if score >= 90 {
    fmt.Println("Grade: A")
} else if score >= 75 {
    fmt.Println("Grade: B")
} else {
    fmt.Println("Grade: C")
}

📤 Output:

Grade: B

🧪 Inline Variable Declaration in If

You can declare and use a variable directly inside the if condition:

if age := 18; age >= 18 {
    fmt.Println("Eligible")
} else {
    fmt.Println("Not eligible")
}

age exists only within the ifelse block.


🛑 Common Mistakes

MistakeFix
Omitting braces {}Go requires braces
Using if (x > 5) like C❌ No parentheses in Go
Declaring var outside scopeUse inline declaration

❌ Invalid:

if (x > 5) {   // ⛔ Parens not allowed

✅ Correct:

if x > 5 {

🧠 Best Practices

✅ Always use braces {} even for one-line blocks
✅ Use short, meaningful condition names
✅ Avoid deeply nested if-else blocks — refactor to improve readability
✅ Use switch when checking multiple discrete values


🧪 Real-World Example – Access Control

user := "admin"
isLoggedIn := true

if user == "admin" && isLoggedIn {
    fmt.Println("Access granted")
} else {
    fmt.Println("Access denied")
}

📤 Output:

Access granted

📌 Summary – Recap & Next Steps

The if statement in Go is a powerful tool for decision-making. Its clean syntax, support for inline variable declarations, and enforced use of braces make Go’s control flow safe and readable.

🔍 Key Takeaways:

  • Use if, else, and else if for conditional logic
  • Go doesn’t allow parentheses around conditions
  • Use inline declaration for scoped variables
  • Prefer clarity over complexity in nested conditions

⚙️ Next: Explore Go Switch Statements for multiple value comparisons in a cleaner syntax.


❓ FAQs – Go If Statement

❓ Can I use parentheses in Go if statements like in C?
✅ No. Go does not require or allow parentheses around conditions.

❓ Can I declare a variable in an if statement?
✅ Yes. Use if x := value; x > 0 for scoped variable declaration.

❓ What happens if the if condition is false and there’s no else?
✅ The code block is skipped, and nothing happens.

❓ How do I write an else if in Go?
✅ Use else if on the same line—Go does not support elif or elseif.

❓ Is a ternary operator like x ? y : z available in Go?
✅ No. Go encourages readability with standard if-else blocks instead.


Share Now :

Leave a Reply

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

Share

Go If Statement

Or Copy Link

CONTENTS
Scroll to Top