0Pricing
Go Academy · Lesson

if Statements and Init Expressions

Conditionals with optional init statements

if in Go

Go's if statement evaluates a boolean expression — no parentheses required, but braces are mandatory:

if x > 0 {
    // positive
}

else and else if

Chain conditions with else if and handle the fallback with else:

func classify(n int) string {
    if n < 0 {
        return "negative"
    } else if n == 0 {
        return "zero"
    } else {
        return "positive"
    }
}

All lessons in this course

  1. if Statements and Init Expressions
  2. The for Loop: All Patterns
  3. switch Statements in Go
  4. break, continue and Labels
← Back to Go Academy