Next Continue in Go

Find this useful? Support us: Star on GitHub 6
Category: Loop | Language: Go

In Go, the Next and Continue keywords are used in loops to control the iteration process.

Next is used to move the iterator to the next item in a collection or sequence. This is commonly used with the for...range statement to iterate over slices, arrays, maps, and channels. Here's an example using Next with a for...range loop:

// Iterate over a slice of integers
numbers := []int{1, 2, 3, 4, 5}
for _, num := range numbers {
    if num == 3 {
        continue // skip 3 and move to the next item
    }
    fmt.Println(num)
}

Output:

1
2
4
5

In this example, the loop skips over the number 3 and continues with the next iteration.

Continue is used to skip the current iteration of a loop and move on to the next. It is commonly used in conjunction with conditionals to skip certain iterations based on a condition. Here's an example:

// Print even numbers from 1 to 10
for i := 1; i <= 10; i++ {
    if i%2 != 0 {
        continue // skip if not even
    }
    fmt.Println(i)
}

Output:

2
4
6
8
10

In this example, the loop skips over odd numbers and only prints the even numbers.