Go for: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Statements")
 
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* Language Specification for Statements https://golang.org/ref/spec#For_statements
=Internal=
=Internal=


* [[Go Concepts - Lexical Structure#Statements|Statements]]
* [[Go Concepts - Lexical Structure#Statements|Statements]]
=Overview=
A <tt>for</tt> statement specifies repeated execution of a block. For more details on blocks, see [[Go Concepts - Lexical Structure#Blocks|Go blocks]].
The iteration is controlled by a condition, a "for" clause, or a "range" clause.
=<tt>for</tt> Controlled by a Condition=
<pre>
for a < b {
    a *= 2
}
</pre>
The condition is evaluated before each iteration. If the condition is absent, is equivalent with <tt>true</tt>.
<pre>
for {
    // do this forever
}
</pre>
=<tt>for</tt> Controlled by a "for" Clause=
<tt>for</tt> controlled by a "for" clause is equivalent with [[#for_Controlled_by_a_Condition|for controlled by a condition]], but it has an extra ''init'' and ''post'' statement:
<pre>
for i := 0; i < 10; i++ {
    // do something
}
</pre>
=<tt>for</tt> Controlled by a "range" Clause=
A <tt>for</tt> 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, <tt>for</tt> exits when all values are returned. For channels, <tt>for</tt> exists when the channel is closed.
<pre>
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
}
</pre>
More about <tt>range</tt>:
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:<tt>[[Go Keyword range|range]]</tt>
</blockquote>
=Iterating over a slice from a non-zero index=
<pre>
for index := 5; index < len(slice); index ++ {
    // slice[index] ...
}
</pre>

Latest revision as of 21:45, 11 April 2016

External

Internal

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] ...
}