Go If Else Statement

An if-else statement runs different blocks of code based on a condition. The program tests whether a condition is true or false and follows the matching path. Decision-making is one of the most fundamental concepts in any programming language.

Basic if Statement

package main

import "fmt"

func main() {
    age := 20

    if age >= 18 {
        fmt.Println("Eligible to vote")
    }
}

Output:

Eligible to vote

if-else Statement

package main

import "fmt"

func main() {
    temperature := 15

    if temperature > 30 {
        fmt.Println("It is hot")
    } else {
        fmt.Println("It is cool")
    }
}

Output:

It is cool

if-else if-else Chain

Use multiple else if blocks to test more than two possible conditions.

package main

import "fmt"

func main() {
    score := 75

    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: F")
    }
}

Output:

Grade: B

Decision Flow Diagram

        score = 75
             │
             ▼
      score >= 90?
      /           \
    Yes            No
     │              │
  "Grade A"    score >= 75?
               /          \
             Yes            No
              │              │
          "Grade B"     score >= 60?
                        /          \
                      Yes            No
                       │              │
                   "Grade C"      "Grade F"

Nested if Statements

An if statement can sit inside another if statement.

package main

import "fmt"

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

    if age >= 18 {
        if hasID {
            fmt.Println("Entry allowed")
        } else {
            fmt.Println("Need valid ID")
        }
    } else {
        fmt.Println("Too young to enter")
    }
}

Output:

Entry allowed

if with Initialization Statement

Go allows declaring a variable inside the if condition. That variable exists only within the if-else block.

package main

import "fmt"

func getScore() int {
    return 85
}

func main() {
    if result := getScore(); result >= 80 {
        fmt.Println("Passed with score:", result)
    } else {
        fmt.Println("Did not pass. Score:", result)
    }
    // result is not accessible here
}

Output:

Passed with score: 85

Logical Operators in Conditions

package main

import "fmt"

func main() {
    username := "admin"
    password := "1234"

    if username == "admin" && password == "1234" {
        fmt.Println("Login successful")
    } else {
        fmt.Println("Invalid credentials")
    }
}

Common Mistakes

MistakeProblemFix
if (x > 5)Parentheses around conditionif x > 5 — no parentheses needed
Opening brace on new lineSyntax error in GoAlways keep { on the same line as if
Using = instead of ==Assignment instead of comparisonUse == to compare values

Key Points

  • No parentheses around the condition — if x > 5 { is correct
  • The opening brace { must stay on the same line as the if keyword
  • Chain multiple checks with else if
  • Variables declared in the if initialization are scoped to that block only
  • Use && and || to combine multiple conditions

Leave a Comment