Summary: in this tutorial, you will learn how to use the Go continue
statement to skip the remaining code in the current iteration of the loop and start the new iteration.
In Go, the continue
statement skips the remaining code of the current iteration inside a loop and moves to the next iteration:
continue
Code language: Go (go)
Typically, you’ll use the continue
statement with an if statement to control the flow of a loop based on a specific condition:
if condition {
continue
}
Code language: Go (go)
In this syntax, if the condition
is true, the continue
statement will run that skips the remaining code in the current iteration and starts a new one.
For example, the following program uses the continue
statement to skip the fmt.Println(i)
line whenever i is even. As a result, it only prints the odd numbers:
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
if i%2 == 0 {
continue
}
fmt.Println(i)
}
}
Code language: Go (go)
Output:
1
3
5
7
9
Code language: Go (go)
Summary
- Use the
continue
statement to skip the remaining code of the current iteration and start a new one.