Saturday, February 7, 2026
Flow Control
Akash AmanUpdated: February 2026
Conditional Logic
The if Statement
Concept: Executes a block of code only if a specific condition is true.
- Behavior: In Go, parentheses
()are omitted for the condition, but curly braces{}are mandatory, even for a single line of code.
The Initialization Statement (Scope Control)
- Behavior: Go allows you to declare and initialize a variable directly within the
ifstatement before the condition is checked.
- Why: This prevents "Variable Pollution." By limiting a variable's life to only where it’s needed, you avoid accidentally using stale or incorrect data later in the function.
The switch Statement
Concept: A streamlined alternative to writing multiple if-else chains.
- Behavior: Unlike languages like C++ or Java, Go has Implicit Break. It executes only the matching case and exits automatically. You don't need to write
breakat the end of every block.
The "Expressionless" Switch
Behavior: If you don't provide a variable to the switch, it acts like a clean if-else if ladder where each case is its own boolean evaluation.
Iteration (Loops)
The for Loop
Concept: Go’s only looping construct. It is a "Swiss Army Knife" that replaces for, while, and do-while from other languages.
Setup:
- Standard:
for i := 0; i < 10; i++ { ... } - While-style:
for condition { ... } - Infinite:
for { ... }
Iterating with range
Concept: The range keyword is the standard way to walk through collections like Slices, Maps, or Strings.
Crucial Warning: The "Copy" Trap
- Behavior: When you use
range, it returns a copy of the element, not a pointer to the original. Modifying the iteration variable does not change the data inside the original slice or map.
Iterating over Maps vs. Strings
- Maps: Iteration is unordered. You may get a different sequence every time you run the loop.
- Strings: Iteration is Unicode-aware. It decodes UTF-8 bytes into
runes(characters) automatically.
Examples
Range over Slices & Arrays
Concept: Iterating through a linear sequence of data.
- Returns:
(index int, value T) - Behavior: Returns two values: the
index(int) and thevalue(the data at that index).
Range over Maps
Concept: Iterating through key-value pairs.
- Returns:
(key K, value V) - Behavior: Returns the
keyand thevalue. However, unlike slices, maps are unordered.
Why: Go intentionally randomizes map iteration order to prevent developers from relying on a specific sequence, which could lead to fragile code that breaks if the underlying map implementation changes.
Range over Strings
Concept: Iterating through text characters.
- Returns:
(index int, char rune) - Behavior: Strings in Go are technically slices of bytes. However,
rangeis Unicode-aware. It automatically decodes UTF-8 bytes intorunes(individual characters).
Why: This ensures that when you loop through a string containing emojis or special characters, you don't accidentally "break" a character by reading only half of its bytes.
Range over Channels
Concept: A way to receive values from a channel until that channel is explicitly closed.
- Return: (value T) — Only the data sent into the channel.
- Setup: Unlike other types,
rangeon a channel only returns one value (the data sent into the channel).
Behavior: The "Waiting" State
- The loop is blocking. If the channel is empty but not closed, the loop will sit and wait for the next value to arrive. If you forget to close the channel, the loop will wait forever, potentially causing a "deadlock."
Why: Clean Termination
- Using
rangeover a channel is the most idiomatic way to "drain" a channel. Instead of manually checking if a channel is open usingval, ok := <-ch, therangeloop automatically handles the exit logic for you the momentclose(ch)is called.
Behavioral Note: You must close the channel from the sender's side. If you don't, the range loop will never finish, and your program might crash or hang.
Advanced Flow Control
The defer Keyword
Concept: Schedules a function to run immediately before the current function returns.
- Why: It is used for "Cleanup Logic." It ensures that resources like files or database connections are closed properly, even if the program crashes (panics).
Behavior: Multiple defer statements follow a LIFO (Last-In, First-Out) order—the last one scheduled is the first one executed.
Loop Control & Labels
Concept: Keywords that modify how a loop behaves mid-execution.
break: Immediately exits the current loop.continue: Skips the rest of the current iteration and starts the next one.
The "Label" (Breaking Nested Loops)
- Behavior: In nested loops, a standard
breakonly exits the inner loop. A Label allows you to break out of the parent loop entirely.
- Why: This avoids "Spaghetti Logic" where you would otherwise need multiple boolean flags to signal the outer loop to stop.
| Keyword | Scope | Result |
|---|---|---|
break | Current Block | Stops the loop/switch and moves to the next code line. |
continue | Current Loop | Skips the current turn and starts the next iteration. |
break LABEL | Labeled Parent | Terminates the specific parent loop associated with the label. |