Go for: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 3: Line 3:
* Language Specification for Statements https://golang.org/ref/spec#For_statements
* Language Specification for Statements https://golang.org/ref/spec#For_statements
=Internal=
=Internal=
* [[Go_Language#for_Loops|Go Language]]
=TODO=


* [[Go Concepts - Lexical Structure#Statements|Statements]]
* [[Go Concepts - Lexical Structure#Statements|Statements]]
The most generic syntax of the <code>for</code> statement is:
<syntaxhighlight lang='go'>
for <init>; <condition>; <update> {
  <statements>
}
</syntaxhighlight>
The <code>for</code> loop executes the <code>init</code> statement and then iterates while the <code>condition</code> evaluates to true. The <code>condition</code> is an expression evaluated on each iteration. <code>condition</code> must evaluate to a boolean. If the expression evaluates to true, the block is executed. At the end of each block execution, the <code>update</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 (for more details, see [[#Short_Variable_Declaration|Short Variable Declaration]] section above):
<syntaxhighlight lang='go'>
for i, j := 0, 0; ... {
  ...
}
</syntaxhighlight>
Examples:
<syntaxhighlight lang='go'>
for i := 0; i < 3; i ++ {
  println(i)
}
</syntaxhighlight>
All three expressions (initialization, condition and update) are optional:
<syntaxhighlight lang='go'>
i := 0
for i < 3 {
  println(i)
  i ++
}
</syntaxhighlight>
<syntaxhighlight lang='go'>
i := 0
for  {
  if i == 3 {
    break
  }
  println(i)
  i ++
}
</syntaxhighlight>
To iterate over an iterable structure, use <code>[[Go_Keyword_range#Overview|range]]</code>:
<syntaxhighlight lang='go'>
ss := []string{"a", "b", "c"}
for index, value := range ss {
  ...
}
</syntaxhighlight>
If the values of the iterable structure are not important, and only the number of elements matters, this syntax can also be used:
<syntaxhighlight lang='go'>
ss := []string{"a", "b", "c"}
for range ss {
  ...
}
</syntaxhighlight>
To get consecutive integers in the loop, use this syntax (Go 1.22 and newer):
<syntaxhighlight lang='go'>
for i := range 10 {
  println(i)
}
</syntaxhighlight>
<code>for</code> can be used to [[Go_Channels#Iterative_Read_from_a_Channel_(Ranging_over_a_Channel)|iterate over values received from a channel]].


=Overview=
=Overview=

Revision as of 00:13, 6 July 2024

External

Internal

TODO


The most generic syntax of the for statement is:

for <init>; <condition>; <update> {
  <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 update statement is executed.

The init statement is usually a variable declaration and initialization. Multiple variables can be declared at the same time (for more details, see Short Variable Declaration section above):

for i, j := 0, 0; ... {
  ...
}

Examples:

for i := 0; i < 3; i ++ {
  println(i)
}

All three expressions (initialization, condition and update) are optional:

i := 0
for i < 3 {
  println(i)
  i ++
}
i := 0
for  {
  if i == 3 {
    break
  }
  println(i)
  i ++
}

To iterate over an iterable structure, use range:

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.

Overview

A for statement specifies repeated execution of a block. For more details on blocks, see Go blocks.

The iteration is controlled by a condition, a "for" clause, or a "range" clause.

for Controlled by a Condition

for a < b {
    a *= 2
}

The condition is evaluated before each iteration. If the condition is absent, is equivalent with true.

for {
    // do this forever
}

for Controlled by a "for" Clause

for controlled by a "for" clause is equivalent with for controlled by a condition, but it has an extra init and post statement:

for i := 0; i < 10; i++ {
    // do something
}

for Controlled by a "range" Clause

A for statement with a "range" clause iterates through all entries of an array, slice, string or map, or values received on a channel. For each entry it 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.

var a [10]string

for i, s := range a { 

	// type of i is int
	// type of s is string
	// and s == a[i]

       // do something
}

More about range:

range

Iterating over a slice from a non-zero index

for index := 5; index < len(slice); index ++ {
    // slice[index] ...
}