Go Decision Making
Go - Decision Making
Section titled “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:
| Statement | Description |
|---|---|
if statement | Executes a block of code only if a specified boolean condition is true. |
if...else statement | Executes one block of code if a condition is true, and another block if the condition is false. |
if...else if...else statement | Tests multiple conditions in sequence. Executes the block corresponding to the first true condition, or the else block if none are true. |
Nested if statements | Placing one if or if...else statement inside another allows for more complex condition checking. |
switch statement | Compares 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 statement | Specifically 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 if Statement
Section titled “The if Statement”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}The if...else Statement
Section titled “The if...else Statement”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}The if...else if...else Statement
Section titled “The if...else if...else Statement”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}The switch Statement
Section titled “The switch Statement”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
caseblock (unlike C/Java). Use thefallthroughkeyword explicitly if needed. - Cases can be expressions, not just constants.
- A
switchwithout an expression is likeswitch true, useful for cleanerif/else iflogic. - Can include an initialization statement like
if.
Syntax:
switch optional_initializer; optional_expression {case value1: // Code for value1case value2, value3: // Multiple values per case // Code for value2 or value3case expression_evaluating_to_value: // Code for matching expressiondefault: // Code if no case matches}The select Statement
Section titled “The select Statement”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 1case 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).