Language 10 of 10 · Topic 0.6

Control Flow: The Unified 'for' Loop & Go 1.22 Range Over Integers

1Concept

Go has only ONE looping keyword: for. It handles standard loops, while loops, infinite loops, and collections traversal. Go 1.22 added for i := range n.

2Architecture Diagram

Standard Loop : for i := 0; i < N; i++ {}
While Loop    : for condition {}
Infinite Loop : for {}
Range Over Int: for i := range 5 {} (Go 1.22)

3Code Example

Stage 0 Language Foundations
package main

import "fmt"

func main() {
    fmt.Println("=== Go 1.22 Range Over Integer ===")
    for i := range 4 {
        fmt.Printf("Step %d ", i)
    }
    fmt.Println()

    fmt.Println("=== Range Over Slice ===")
    stacks := []string{"Go", "Kubernetes", "gRPC"}
    for idx, s := range stacks {
        fmt.Printf("#%d: %s\n", idx+1, s)
    }
}

4Expected Output

=== Go 1.22 Range Over Integer ===
Step 0 Step 1 Step 2 Step 3 
=== Range Over Slice ===
#1: Go
#2: Kubernetes
#3: gRPC

5Key Takeaways

  • Go has no 'while' or 'do-while' keyword; 'for' handles all looping.
  • Go 1.22 supports 'for i := range 10' directly.
  • range yields index and value (use _ to ignore index).