Go loop - break and continue

Intro

break will break out of a loop. It’s a way to stop looping.

continue will move on to the next iteration. Let’s see it in action.

Examples

Example - break

package main

import "fmt"

func main() {
    pow := make([]int, 10)
    for i := range pow {
        pow[i] = 1 << uint(i)
        if pow[i] >= 16 {
            break
        }
    }
    fmt.Println(pow)
    // [1 2 4 8 16 0 0 0 0 0]
}

Example - continue

package main

import "fmt"

func main() {
    pow := make([]int, 10)
    for i := range pow {
        if i%2 == 0 {
            continue
        }
        pow[i] = 1 << uint(i)
    }
    fmt.Println(pow)
    // [0 2 0 8 0 32 0 128 0 512]
}

http://www.golangbootcamp.com/book/collection_types


See also