0
0
GoHow-ToBeginner · 3 min read

How to Use If Else in Go: Simple Guide with Examples

In Go, use if to run code when a condition is true, and else to run code when it is false. The syntax is simple: start with if condition { }, and optionally add else { } after it.
📐

Syntax

The if statement checks a condition (parentheses are not used in Go) and runs the code block if true. The else block runs if the if condition is false. You can also use else if to check multiple conditions.

  • if condition { }: Runs code if condition is true.
  • else { }: Runs code if previous if or else if conditions are false.
  • else if condition { }: Checks another condition if the first if is false.
go
if condition {
    // code runs if condition is true
} else if anotherCondition {
    // code runs if anotherCondition is true
} else {
    // code runs if none of the above conditions are true
}
💻

Example

This example shows how to check a number and print if it is positive, negative, or zero using if, else if, and else.

go
package main

import "fmt"

func main() {
    number := 5

    if number > 0 {
        fmt.Println("The number is positive.")
    } else if number < 0 {
        fmt.Println("The number is negative.")
    } else {
        fmt.Println("The number is zero.")
    }
}
Output
The number is positive.
⚠️

Common Pitfalls

Common mistakes include forgetting the curly braces { }, omitting the condition, or using parentheses around the condition (which are not used in Go). Also, Go requires the condition to be a boolean expression.

Another pitfall is not using else if properly and instead writing multiple separate if statements, which can cause all conditions to be checked instead of stopping at the first true one.

go
package main

import "fmt"

func main() {
    x := 10

    // Wrong: missing braces
    // if x > 5
    //     fmt.Println("x is greater than 5")

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

    // Wrong: multiple ifs instead of else if
    if x > 0 {
        fmt.Println("x is positive")
    }
    if x > 5 {
        fmt.Println("x is greater than 5")
    }

    // Correct:
    if x > 0 {
        fmt.Println("x is positive")
    } else if x > 5 {
        fmt.Println("x is greater than 5")
    }
}
Output
x is greater than 5 x is positive x is greater than 5
📊

Quick Reference

Remember these tips when using if else in Go:

  • Curly braces { } are required even for single statements.
  • Conditions must be boolean expressions (true or false).
  • Parentheses around conditions are not used in Go.
  • Use else if to chain multiple conditions.
  • Indent code inside blocks for readability.

Key Takeaways

Use if to run code when a condition is true and else for the false case.
Always include curly braces { } around the code blocks.
Use else if to check multiple conditions in order.
Conditions must be boolean expressions without needing parentheses.
Avoid multiple separate if statements when else if is more appropriate.