← Back to Go Course | Chapter 3: Control Flow | Lesson 2 of 7

if / else Statements

An if/else statement lets your program make a decision, like choosing which path to walk down a fork in the road.
Syntax
go
if condition {
    // body
} else if condition2 {
    // body
} else {
    // body
}

if init; condition {
    // body
}

Basic if / else

An if statement runs a block only when its condition evaluates to true, and an optional else block runs otherwise. Unlike C or Java, the condition is never wrapped in parentheses, and the braces are always required, even for a single statement.

Note: Go never requires (or wants) parentheses around the condition.

Example: Basic if / else

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create age and infer the type
	age := 20
	// Check whether age >= 18
	if age >= 18 {
		// Print the values followed by a newline
		fmt.Println("adult")
	} else {
		// Print the values followed by a newline
		fmt.Println("minor")
	}
}

else if Chains

Multiple conditions can be chained with 'else if', evaluated top to bottom until one is true, falling through to a final else if none match -- useful for handling several mutually exclusive cases.

Example: else if Chains

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create score and infer the type
	score := 72
	// Check whether score >= 90
	if score >= 90 {
		// Print the values followed by a newline
		fmt.Println("Grade A")
	} else if score >= 75 {
		// Print the values followed by a newline
		fmt.Println("Grade B")
	} else if score >= 60 {
		fmt.Println("Grade C")
	} else {
		fmt.Println("Grade F")
	}
}

if with an Init Statement

Go lets you run a short statement before the condition, separated by a semicolon, most often used to assign a value and immediately test it -- a pattern very common with functions that return a value and an error.

Note: Variables declared in an if's init statement are only visible within that if/else chain.

Example: if with an Init Statement

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function half
func half(n int) (int, bool) {
	// Check whether n%2 != 0
	if n%2 != 0 {
		// Send 0, false back to the caller
		return 0, false
	}
	// Send n / 2, true back to the caller
	return n / 2, true
}

// main is where the program starts running
func main() {
	// Check whether result, ok := half(10); ok
	if result, ok := half(10); ok {
		fmt.Println("half is:", result)
	} else {
		fmt.Println("not evenly divisible")
	}
}

Boolean Expressions

Conditions combine comparisons with && (and), || (or), and ! (not), just like most C-family languages, and Go short-circuits these operators, stopping evaluation as soon as the result is determined.

Example: Boolean Expressions

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create age and infer the type
	age := 25
	// Short declaration: create hasID and infer the type
	hasID := true
	// Check whether age >= 18 && hasID
	if age >= 18 && hasID {
		// Print the values followed by a newline
		fmt.Println("entry allowed")
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Wrapping the condition in parentheses like 'if (x > 5)' -- Go allows it but idiomatic style always omits them.
  2. Forgetting that the opening brace must be on the same line as if/else -- Go's automatic semicolon insertion breaks otherwise.
  3. Not realizing a variable declared in the if's init statement (if x := f(); x > 0) is scoped only to the if/else chain, not outside it.
Chapter Summary
  • if/else branches based on a boolean condition, with no parentheses required around the condition.
  • An optional init statement can run before the condition, scoping a variable to the whole if/else chain.
  • else if chains multiple conditions together.
  • Braces are mandatory in Go, unlike some C-family languages that allow single-statement bodies without them.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.