Skip to content

Go Decision Making

Decision-making structures are essential for controlling the flow of execution in a program. They allow you to specify conditions that are evaluated by the program, and based on whether these conditions are true or false, different blocks of code are executed.

Essentially, you tell the program: “If this condition is true, do this; otherwise, maybe do that, or do something else.”

Go provides several statements for decision making:

StatementDescription
if statementExecutes a block of code only if a specified boolean condition is true.
if...else statementExecutes one block of code if a condition is true, and another block if the condition is false.
if...else if...else statementTests multiple conditions in sequence. Executes the block corresponding to the first true condition, or the else block if none are true.
Nested if statementsPlacing one if or if...else statement inside another allows for more complex condition checking.
switch statementCompares an expression against a list of possible constant values (cases). Executes the block associated with the matching case. Go’s switch is more flexible than in many other languages.
select statementSpecifically designed for concurrency. Waits on multiple channel operations and executes the block associated with the first channel operation that is ready. Similar in structure to switch but used for communication.

The basic if statement executes code only when its condition evaluates to true.

Syntax:

if boolean_expression {
// Code executes if boolean_expression is true
}

Go’s if statements (and switch) can also include a short initialization statement executed before the condition. Variables declared here are scoped to the if (and any else if/else) blocks.

Syntax with initializer:

if initialization_statement; boolean_expression {
// Code executes if boolean_expression is true
// Variables from initialization_statement are accessible here
}

Example:

package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
// If with initializer
if num := rand.Intn(100); num >= 50 {
fmt.Printf("Got %d, which is >= 50\n", num)
// 'num' is accessible here
} else {
fmt.Printf("Got %d, which is < 50\n", num)
// 'num' is also accessible here
}
// fmt.Println(num) // Error: 'num' is not accessible outside the if/else blocks
}

Executes one block if the condition is true, and another if it’s false.

Syntax:

if boolean_expression {
// Executes if condition is true
} else {
// Executes if condition is false
}

Allows checking multiple conditions.

Syntax:

if boolean_expression_1 {
// Executes if condition 1 is true
} else if boolean_expression_2 {
// Executes if condition 1 is false AND condition 2 is true
} else {
// Executes if all preceding conditions are false
}

Provides a multi-way branching alternative to long if...else if chains. It compares an expression against multiple case values.

Key features in Go:

  • No automatic fallthrough: Execution stops after a matching case block (unlike C/Java). Use the fallthrough keyword explicitly if needed.
  • Cases can be expressions, not just constants.
  • A switch without an expression is like switch true, useful for cleaner if/else if logic.
  • Can include an initialization statement like if.

Syntax:

switch optional_initializer; optional_expression {
case value1:
// Code for value1
case value2, value3: // Multiple values per case
// Code for value2 or value3
case expression_evaluating_to_value:
// Code for matching expression
default:
// Code if no case matches
}

Used exclusively with channel operations (sending or receiving). It blocks until one of its case operations can proceed, then executes that case. If multiple cases are ready, one is chosen randomly. A default case executes immediately if no other channels are ready.

Syntax:

select {
case communication_clause1:
// Statement(s) for case 1
case communication_clause2:
// Statement(s) for case 2
...
default: // Optional
// Statement(s) if no other communication is ready
}

Detailed examples for each statement type can be found in subsequent sections or by following the links (if available).