Essential Go for, while loops  Suggest an edit

break and continue

Break

Break allows to exit for loop early.

i := 0
for {
	i++
	if i > 2 {
		break
	}
	fmt.Printf("i: %d\n", i)
}
i: 1
i: 2

Continue

Continue starts new iteration of for loop.

for i := 0; i < 4; i++ {
	if i < 2 {
		continue
	}
	fmt.Printf("i: %d\n", i)
}
i: 2
i: 3

Break/continue loop inside switch

import "fmt"

func main() {
    j := 100

loop:
    for j < 110 {
        j++

        switch j % 3 {
        case 0:
            continue loop
        case 1:
            break loop
        }

        fmt.Println("Var : ", j)
    }
}
  ↑ ↓ to navigate     ↵ to select     Esc to close