Start learning free

LESSON 67 OF 338 · CONDITIONAL STATEMENTS

If Statements in Go: Running Code Only When a Condition Holds

Lesson Read the full explanation

An if statement lets a program run code only when a condition is true.

In Go, the condition is written after if, and the body is wrapped in curly braces:

passed := true

if passed {
    fmt.Println("Passed the test")
}

You can add else to run different code when the condition is false:

if passed {
    fmt.Println("Passed the test")
} else {
    fmt.Println("Failed")
}

Assignment
Follow the Coding Tutorial and print the correct message based on passed.


STEP 1 OF 3
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    If Statements in Go, explained

    What an if statement does

    A program usually has to react to its data: print one message for a passing grade and another for a failing one, skip work that isn't needed, or stop on bad input. The if statement makes that choice. It evaluates a condition, and the block after it runs only when the condition is true.

    passed := true
    
    if passed {
        fmt.Println("Passed the test")
    }
    

    Because passed is true, this prints Passed the test. With passed := false the block is skipped and nothing is printed.

    The syntax

    Go's if has three features worth noticing right away:

    • No parentheses around the condition. Write if passed {, not if (passed) {. Parentheses are allowed but gofmt, Go's standard formatter, removes them.
    • Braces are mandatory. Even a one-line body needs { and }. There is no brace-free form.
    • The opening brace stays on the same line as the if.

    The condition must be a boolean

    Go has no "truthy" or "falsy" values. A number, a string or a slice can't stand in for a condition. The condition must have type bool, which means a boolean variable or a comparison such as score >= 5. This code does not compile:

    n := 1
    if n {
        fmt.Println("a")
    }
    

    The compiler reports non-boolean condition in if statement. If you meant "n is not zero", write if n != 0.

    When the variable is already a bool, use it directly. if passed { is the idiomatic form; if passed == true { works but says the same thing twice.

    Adding else

    An else block runs when the condition is false. Exactly one of the two blocks runs, never both and never neither:

    passed := false
    
    if passed {
        fmt.Println("Passed the test")
    } else {
        fmt.Println("Failed")
    }
    

    This prints Failed. An else has no condition of its own. It covers every case the if did not.

    Where else goes

    Write } else { on one line. The closing brace of the if block, the word else and the opening brace of the next block all share a line. Go inserts a semicolon automatically at the end of a line that ends in }, so an else that starts a new line looks like a stray statement. This program:

    if passed {
        fmt.Println("a")
    }
    else {
        fmt.Println("b")
    }
    

    fails with syntax error: unexpected else, expected }.

    A variable scoped to the if

    An if may begin with a short statement, separated from the condition by a semicolon. A variable declared there exists only inside the if and its else branches:

    if n := 7; n%2 == 0 {
        fmt.Println("even")
    } else if n > 5 {
        fmt.Println("odd and big", n)
    } else {
        fmt.Println("odd and small")
    }
    

    This prints odd and big 7. The else if in the middle adds a second condition that is checked only when the first one failed. After the whole statement, n is no longer available.

    The exercise

    The lesson starts with passed := true and asks you to print the right message. Work through it in three moves:

    1. Write the if. The condition is just passed, since it already holds a boolean.
    2. Print the success message inside the block with fmt.Println("Passed the test"). Println adds the line break for you.
    3. Add the else on the closing-brace line and print "Failed" inside it.

    The finished code:

    passed := true
    if passed {
        fmt.Println("Passed the test")
    } else {
        fmt.Println("Failed")
    }
    

    With passed set to true, the lesson's tests expect Passed the test. Change the value to false in the editor above to see the other branch.

    Common mistakes

    • Putting else on its own line, which is a syntax error as shown above.
    • Using = instead of == in a condition. = assigns and == compares. Go does not allow an assignment as a condition, so the compiler stops you.
    • Using a non-boolean as the condition, such as if n {.
    • Declaring a variable and never using it. Go rejects unused local variables, so a leftover x := 1 stops the build.

    Key takeaways

    • The block after if runs only when the condition is true.
    • No parentheses around the condition, but the braces are required.
    • The condition must be a bool: no truthy or falsy values.
    • Write } else { on one line; else has no condition.
    • A short statement before the condition keeps its variable scoped to the if.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free