Skip to main content

Overview

Classic Loop

for i := 0; i < 5; i++ {
    fmt.Println(i)
}

// 0
// 1
// 2
// 3
// 4

While Loop

i := 0
for i < 5 {
    fmt.Println(i)
    i++
}

// 0
// 1
// 2
// 3
// 4

Infinite Loop

for {
    fmt.Println("running")
}

// running
// running
// running
// running
// ... (infinite)

Range Loop

Used to loop over integers, strings (runes), arrays, slices, maps, and channels.
for i := range 3 {
    fmt.Println(i)
}

// 0
// 1
// 2
nums := []int{10, 20, 30}

for i, v := range nums {
    fmt.Println(i, v)
}

// 0 10
// 1 20
// 2 30

Control Keywords

Break

Stops the loop completely.
for i := 0; i < 5; i++ {
    if i == 2 {
        break
    }

    fmt.Println(i)
}

// 0
// 1

Continue

Skips to the next iteration.
for i := 0; i < 5; i++ {
    if i == 2 {
        continue
    }

    fmt.Println(i)
}

// 0
// 1
// 3
// 4

Labeled Loops

A labeled loop is a loop with a name that lets break or continue target a specific loop when loops are nested.
outerLoop:
    for i := 0; i < 3; i++ {
        for j := 0; j < 3; j++ {
            if i == j {
                continue outerLoop
            }

            fmt.Println(i, j)
        }
    }

// 1 0
// 2 0
// 2 1

Build docs developers (and LLMs) love