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 {, notif (passed) {. Parentheses are allowed butgofmt, 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:
- Write the
if. The condition is justpassed, since it already holds a boolean. - Print the success message inside the block with
fmt.Println("Passed the test").Printlnadds the line break for you. - Add the
elseon 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
elseon 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 := 1stops the build.
Key takeaways
- The block after
ifruns only when the condition istrue. - 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;elsehas no condition. - A short statement before the condition keeps its variable scoped to the
if.