Skip to main content

Conditionals

4 minutes read•Filed underGo Programming Languageon

Master Go's conditional statements — if, else, else if, and if init statements. Learn how Go enforces strict boolean conditions and how scoped variables make error handling cleaner.

The if statement

An if statement executes a block of code only when a condition is true. The condition is a boolean expression — it must evaluate to either true or false.

The parentheses around the condition are optional in many languages but are absent by convention in Go — the compiler accepts them, but gofmt removes them. The curly braces, however, are always required. Go does not allow single-line if bodies without braces.

The if-else statement

An else block runs when the condition is false. It gives you a second path of execution — exactly one of the two blocks will run:

The else keyword must appear on the same line as the closing brace of the if block. Putting it on the next line is a compile error because Go's automatic semicolon insertion places a semicolon after the }, making the else unreachable:

The if-else if statement

When you need to evaluate more than two conditions, you can chain else if clauses. Go evaluates them from top to bottom and executes the first block whose condition is true. If no condition matches and an else is present, that block runs instead:

Once a condition matches, the remaining branches are skipped entirely — Go does not fall through to subsequent else if blocks. The final else is optional; without it, if no condition matches, execution continues after the entire chain.

If with an init statement

Go allows an optional initialization statement before the condition, separated by a semicolon. The variable declared in the init statement is scoped to the entire if-else block — it exists only within that block and is not accessible after it:

This is not just syntactic convenience — it is an intentional scoping mechanism. By declaring the variable inside the if, you signal that its purpose is exclusively tied to that conditional check. It cannot leak into the surrounding scope and affect unrelated code.

The pattern is used constantly for error handling in Go. Functions that can fail return a value and an error. The init statement captures both, and the body handles the failure path:

This is the idiomatic Go way to handle errors inline, without declaring a throwaway variable in the outer scope just to check it once. The variable exists for exactly as long as it is needed — no more.