Go Lesson 11 – If-Else Statements | Dataplexa

If-Else Statements in Go

Decision-making is a core part of every program. In Go, if-else statements are used to execute code based on conditions.

Go keeps conditionals clean and readable by avoiding unnecessary syntax such as parentheses around conditions.


Basic If Statement

The if statement executes a block of code only if a condition evaluates to true.

package main

import "fmt"

func main() {
    age := 20

    if age >= 18 {
        fmt.Println("You are eligible to vote")
    }
}

If the condition is false, the code inside the block is skipped.


If-Else Statement

Use else when you want to run an alternative block if the condition is false.

package main

import "fmt"

func main() {
    balance := 500

    if balance >= 1000 {
        fmt.Println("Sufficient balance")
    } else {
        fmt.Println("Insufficient balance")
    }
}

Only one block executes depending on the condition result.


If – Else If – Else

When multiple conditions need to be checked in sequence, use else if.

package main

import "fmt"

func main() {
    score := 82

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

Conditions are evaluated from top to bottom. Once a condition is true, the remaining checks are skipped.


Using Logical Operators

Go supports logical operators such as:

  • && (AND)
  • || (OR)
  • ! (NOT)
package main

import "fmt"

func main() {
    age := 25
    hasID := true

    if age >= 18 && hasID {
        fmt.Println("Entry allowed")
    } else {
        fmt.Println("Entry denied")
    }
}

If Statement with Initialization

Go allows declaring and initializing variables inside the if statement. These variables exist only within the conditional block.

package main

import "fmt"

func main() {
    if temperature := 32; temperature > 30 {
        fmt.Println("Hot weather")
    } else {
        fmt.Println("Normal weather")
    }
}

This improves code readability and limits variable scope.


Real-World Example: Loan Approval

Let’s apply conditionals to a real banking scenario.

package main

import "fmt"

func main() {
    salary := 55000
    creditScore := 720

    if salary >= 50000 && creditScore >= 700 {
        fmt.Println("Loan approved")
    } else {
        fmt.Println("Loan rejected")
    }
}

Multiple conditions help make accurate decisions in real systems.


Common Mistakes with If Statements

  • Using parentheses around conditions (not required)
  • Forgetting braces { }
  • Expecting multiple blocks to run

Best Practices

  • Keep conditions simple and readable
  • Use else if instead of nested ifs
  • Prefer early checks for error handling

Why Go’s If Statements Are Powerful

Go’s conditional syntax encourages:

  • Clean logic flow
  • Reduced nesting
  • Maintainable large-scale code

What’s Next?

In the next lesson, you will learn about Switch Statements in Go, which provide a cleaner alternative to multiple if-else blocks.