Go controls the flow of execution with if, else, and switch. An if runs a block when a boolean condition is true, and a switch picks one branch out of several by comparing a value or evaluating a list of conditions. Go has no ternary operator and no parentheses around conditions, which keeps the syntax small and consistent.
Both statements can start with a short statement such as v, err := f(), whose variables are scoped to the whole statement. This pattern is the foundation of idiomatic error handling in Go.
if and else workif statement with an init clause
if and else workThe condition must be a bool. Go does not treat numbers, strings, or pointers as truthy or falsy, so if n { is a compile error when n is an int. Braces are always required, even for a single statement, and the opening brace must be on the same line as if.
Conditions are evaluated top to bottom and the first true one wins. The && and || operators short-circuit, so p != nil && p.Ready is safe because p.Ready is only evaluated when p is not nil.
if statement with an init clauseAn if may begin with a simple statement, separated from the condition by a semicolon. Variables declared there are visible in the if and in every else branch, but not after the statement. This keeps temporary values from leaking into the surrounding function.
The same shape is used for the comma-ok idiom with maps and type assertions:
Idiomatic Go handles the error or unusual case first and returns, so the happy path stays at the left margin. Avoid an else after a block that ends in return, break, or continue.
A switch compares one expression against a list of case values. Cases are evaluated top to bottom, the first match runs, and execution then leaves the switch. There is no implicit fall-through, so you do not write break after each case.
Several values can share one case with a comma-separated list. The default clause runs when nothing matches and may appear anywhere in the switch, although placing it last is the convention. Case values do not have to be constants, and the types must be comparable with the switch expression.
fallthroughIf you do want the next case body to run, end a case with fallthrough. It transfers control to the next case without testing its condition, and it cannot be used in the last case. It is rarely needed; a comma-separated case list is usually clearer.
A switch with no expression is the same as switch true. Each case is a boolean condition, which makes long if / else if chains easier to read.
A type switch compares the dynamic type of an interface value. It is the standard way to branch on what an any actually holds.
Inside each case, x has the type named in that case. In a case that lists several types, or in default, x keeps the interface type of the original value.
| Situation | Use |
|---|---|
| One condition, or an error check | if |
| Setup value used only in the condition | if with an init clause |
| One value compared against many constants | expression switch |
| Several unrelated range checks | tagless switch |
| Branching on the concrete type of an interface | type switch |
| Waiting on several channel operations | select (a separate statement) |
Note that select looks like switch but only works with channel communication. It is covered separately from the conditional statements above.
No. Use a plain if statement, or a small helper function. Since Go 1.21 you can use the built-in min and max for the common numeric cases.
break in a Go switch case?No. Each case ends automatically. Use break only to leave a switch early from inside a nested condition, and use fallthrough when you need the opposite behaviour.
Not directly. Use a tagless switch with comparisons such as case n >= 10 && n < 20:, or list several constants separated by commas.
No. They are scoped to the if statement and all of its else branches. Declare the variable before the if when you need it afterwards.
Use if for single conditions and for error checks, with an init clause to keep temporary variables local. Use switch when you compare one value against many cases, a tagless switch for chains of conditions, and a type switch to branch on interface types. Cases never fall through unless you ask for it, and conditions must always be real booleans.