The if statement

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

Go
age := 20if age >= 18 {    fmt.Println("adult")}

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 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, and exactly one of the two blocks runs:

Go
score := 72if score >= 60 {    fmt.Println("pass")} else {    fmt.Println("fail")}

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:

Go
// compile error: unexpected elseif score >= 60 {    fmt.Println("pass")}else {    fmt.Println("fail")}

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:

Go
temp := 22if temp < 0 {    fmt.Println("freezing")} else if temp < 15 {    fmt.Println("cold")} else if temp < 25 {    fmt.Println("comfortable")} else {    fmt.Println("hot")}

Once a condition matches, Go skips the remaining branches entirely and 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:

Go
if x := compute(); x > 0 {    fmt.Println("positive:", x)} else {    fmt.Println("non-positive:", x)}// x is not accessible here

This scoping is intentional. 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:

Go
if err := doSomething(); err != nil {    fmt.Println("error:", err)    return}

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 goes out of scope as soon as the check is done.