Go Keyword range: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
=TO PROCESS=
=TO PROCESS=


=Internal=
==Internal==


* [[Go_Concepts_-_Lexical_Structure#Keywords|Keywords]]
* [[Go_Concepts_-_Lexical_Structure#Keywords|Keywords]]
Line 7: Line 7:
* [[Go Channels|Channels]]
* [[Go Channels|Channels]]


=Overview=
==Overview==


<tt>range</tt> keyword is used to iterate over strings, [[Go_Arrays#Iterating_over_Arrays|arrays]], [[Go_Slices#Iterating_over_Slices|slices]], [[Go_Maps#Iterating_over_Maps|maps]], channels and [[Go_Concepts_-_Functions#Varidic_Functions|variadic function arguments]]. It returns two values. On the first position is the index/key, and on the second position is the ''copy'' of the value in that element. For slices and arrays, <tt>range</tt> always starts to iterate from index 0. If you need more control over the start index, use a <tt>[[Go_for#Iterating_over_a_slice_from_a_non-zero_index|for]]</tt> loop.
<tt>range</tt> keyword is used to iterate over strings, [[Go_Arrays#Iterating_over_Arrays|arrays]], [[Go_Slices#Iterating_over_Slices|slices]], [[Go_Maps#Iterating_over_Maps|maps]], channels and [[Go_Concepts_-_Functions#Varidic_Functions|variadic function arguments]]. It returns two values. On the first position is the index/key, and on the second position is the ''copy'' of the value in that element. For slices and arrays, <tt>range</tt> always starts to iterate from index 0. If you need more control over the start index, use a <tt>[[Go_for#Iterating_over_a_slice_from_a_non-zero_index|for]]</tt> loop.
Line 19: Line 19:
</blockquote>
</blockquote>


=Iterating over Indices=
==Iterating over Indices==


<pre>
<pre>
Line 28: Line 28:
</pre>
</pre>


=Iterating over Values=
==Iterating over Values==


<pre>
<pre>
Line 37: Line 37:
</pre>
</pre>


=Iterating over Indices and Values=
==Iterating over Indices and Values==


<pre>
<pre>
Line 47: Line 47:
</pre>
</pre>


=Iterating over Key/Values in a Map=
==Iterating over Key/Values in a Map==


<pre>
<pre>
Line 61: Line 61:
</pre>
</pre>


=<tt>range</tt> and Channels=
==<tt>range</tt> and Channels==


<tt>range</tt> can be used to [[Go_Channels#Receiving_from_a_Channel|receive from a channel]] in a <tt>for</tt> loop. <tt>range</tt> blocks until a value is available on the channel. The iteration values produced are the successive values sent on the channel until the channel is closed and all values are consumed. When the channel is closed, the <tt>for</tt> loop exists:
<tt>range</tt> can be used to [[Go_Channels#Receiving_from_a_Channel|receive from a channel]] in a <tt>for</tt> loop. <tt>range</tt> blocks until a value is available on the channel. The iteration values produced are the successive values sent on the channel until the channel is closed and all values are consumed. When the channel is closed, the <tt>for</tt> loop exists:

Revision as of 01:01, 22 August 2023

TO PROCESS

Internal

Overview

range keyword is used to iterate over strings, arrays, slices, maps, channels and variadic function arguments. It returns two values. On the first position is the index/key, and on the second position is the copy of the value in that element. For slices and arrays, range always starts to iterate from index 0. If you need more control over the start index, use a for loop.

Note 1: that only a first identifier is declared, that is the index and not the value, as the intuition would suggest. See examples below.
Note 2: range returns a copy of the value, in the same way it would pass an argument to a function. It does NOT return a reference to the element in the structure it iterates over.

Iterating over Indices

var a [5]int
for i := range a {
   // i is the index
}

Iterating over Values

var a [5]int
for _, value := range a {
   // 'value' is the value
}

Iterating over Indices and Values

var a [5]int
for i, value := range a {
   // i is the index
   // 'value' is the value
}

Iterating over Key/Values in a Map

m := map[string]string { 
    "k1": "v1",
    "k2": "v2",
    "k3": "v3",
}

for key, value := range m {
    // 
}

range and Channels

range can be used to receive from a channel in a for loop. range blocks until a value is available on the channel. The iteration values produced are the successive values sent on the channel until the channel is closed and all values are consumed. When the channel is closed, the for loop exists:

var c chan string

for m := range c {
   // do something with the message
}

If the channel is nil, the range expression blocks forever.