100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

if-else Statements in Go

Learn how Go's if-else statement works without parentheses, including its unique init-statement syntax.

Control FlowBeginner6 min readJul 8, 2026
Analogies

Introduction

The if-else statement in Go lets you execute code conditionally based on a boolean expression. Go's version is deliberately minimal: no parentheses around the condition are required, but curly braces are always mandatory, even for single-statement bodies. This keeps code visually consistent across the entire language ecosystem, since gofmt enforces brace placement automatically.

🏏

Cricket analogy: Go's mandatory curly braces for if-else are like the strict, non-negotiable follow-through rule umpires enforce on every bowler's action, applied consistently even for a single simple delivery.

Syntax

go
if condition {
    // executes when condition is true
} else if anotherCondition {
    // executes when anotherCondition is true
} else {
    // executes otherwise
}

// with an init statement
if result := compute(); result > 0 {
    // result is scoped to this if-else chain only
}

Explanation

The condition must evaluate to a boolean; Go does not allow implicit conversion from integers or other types like C does. The opening brace must be on the same line as the if, else if, or else keyword, otherwise the compiler inserts a semicolon automatically and breaks the chain. Go also supports an optional init statement before the condition, separated by a semicolon. Any variable declared there is scoped only to the if-else block, which is a handy way to keep helper variables out of the surrounding function scope.

🏏

Cricket analogy: Go disallowing implicit int-to-boolean conversion is like an umpire refusing to treat "three appeals" as automatically meaning "out"; the condition must explicitly be a genuine dismissal, not just any nonzero number of appeals.

Example

go
package main

import "fmt"

func classify(score int) string {
    if score >= 90 {
        return "A"
    } else if score >= 75 {
        return "B"
    } else {
        return "C"
    }
}

func main() {
    scores := []int{95, 80, 60}
    for _, s := range scores {
        if grade := classify(s); grade == "A" {
            fmt.Println(s, "-> top grade:", grade)
        } else {
            fmt.Println(s, "-> grade:", grade)
        }
    }
}

Output

go
95 -> top grade: A
80 -> grade: B
60 -> grade: C

Key Takeaways

  • Parentheses around the condition are optional; braces are always required.
  • The opening brace must stay on the same line as if/else if/else.
  • The condition must be a genuine boolean expression.
  • An init statement can declare variables scoped to the whole if-else chain.
  • else and else if must start on the same line as the preceding closing brace.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#IfElseStatementsInGo#Else#Statements#Syntax#Explanation#StudyNotes#SkillVeris