Go for: Difference between revisions
Line 19: | Line 19: | ||
The <code>for</code> loop executes the <code>init</code> statement and then iterates while the <code>condition</code> evaluates to <code>true</code>. The <code>condition</code> is an expression evaluated on each iteration. <code>condition</code> must evaluate to a boolean. If the expression evaluates to <code>true</code>, the block is executed. At the end of each block execution, the <code>post</code> statement is executed. | The <code>for</code> loop executes the <code>init</code> statement and then iterates while the <code>condition</code> evaluates to <code>true</code>. The <code>condition</code> is an expression evaluated on each iteration. <code>condition</code> must evaluate to a boolean. If the expression evaluates to <code>true</code>, the block is executed. At the end of each block execution, the <code>post</code> statement is executed. | ||
The <code>init</code> statement is usually a variable declaration and initialization. Multiple variables can be declared at the same time ( | The <code>init</code> statement is usually a variable declaration and initialization. Multiple variables can be declared at the same time (see [[Go_Language#Short_Variable_Declaration|Short Variable Declaration]]): | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
for i, j := 0, 0; ... { | for i, j := 0, 0; ... { |
Revision as of 23:49, 12 August 2024
External
- Language Specification for Statements https://golang.org/ref/spec#For_statements
Internal
Overview
There are four for
syntactical forms: the generic, C-like syntax with an init section, a condition and post section, a form where the iteration is controlled by a range
keyword, a form with just one condition, and a form that iterates forever, unless the body contains statements that exit the loop. There are no do
or while
loops in Go. In all four causes, the for
statement specifies repeated execution of a block.
for init; condition; post {}
The most generic syntax of the for
statement is a C-like syntax, with an init, condition and post statements:
for <init>; <condition>; <post> { <statements> }
The for
loop executes the init
statement and then iterates while the condition
evaluates to true
. The condition
is an expression evaluated on each iteration. condition
must evaluate to a boolean. If the expression evaluates to true
, the block is executed. At the end of each block execution, the post
statement is executed.
The init
statement is usually a variable declaration and initialization. Multiple variables can be declared at the same time (see Short Variable Declaration):
for i, j := 0, 0; ... {
...
}
Examples:
for i := 0; i < 3; i ++ {
println(i)
}
All of the init
, condition
and post
sections are optional and can be dropped. If we drop the init
and the post
section, we get the for <condition>
form. If we drop all of them, we get the for {}
form.
for range {}
To iterate over an iterable structure, such as an array, slice, string or map, or values received on a channel, use range
. For each entry, range
assigns iteration values to corresponding iteration variables if present and then executes the block. For arrays, maps and slices, for
exits when all values are returned. For channels, for
exists when the channel is closed.
ss := []string{"a", "b", "c"}
for index, value := range ss {
...
}
If the values of the iterable structure are not important, and only the number of elements matters, this syntax can also be used:
ss := []string{"a", "b", "c"}
for range ss {
...
}
To get consecutive integers in the loop, use this syntax (Go 1.22 and newer):
for i := range 10 {
println(i)
}
for
can be used to iterate over values received from a channel.
for condition {}
for <condition> { <statements> }
The condition is evaluated before each iteration. If the condition is absent, is equivalent with true, and the form syntax becomes for {}
. for condition {}
is equivalent with while
in other languages.
i := 0
for i < 3 {
println(i)
i ++
}
for {}
i := 0
for {
if i == 3 {
break
}
println(i)
i ++
}
Use Cases
Iterating over a slice from a non-zero index
for index := 5; index < len(slice); index ++ {
// slice[index] ...
}