Go Keyword range: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(32 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
 
* [[Go_Language#range_keyword|Go Language]]
* [[Go_Concepts_-_Lexical_Structure#Keywords|Keywords]]
* <tt>[[Go for#for_Controlled_by_a_.22range.22_Clause|for]]</tt>
 
=Overview=
=Overview=
<code>range</code> can be used to iterated through [[Go_Channels#Iterative_Read_from_a_Channel|channels]].


<tt>range</tt> keyword is used to iterate over strings, arrays, slices, 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.
=Iterating through Arrays=
{{Internal|Go_Arrays#Iterating_though_Arrays|Iterating though Arrays}}
=Iterating through Slices=
{{Internal|Go_Slices#Iterating_through_Slices|Iterating through Slices}}
=Iterating over Map Keys and Values=
{{Internal|Go_Maps#Iterating_over_a_Map|Iterating over Map Keys and Values}}


<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
=TO PROCESS=
:Note that only a first identifier is declared, that is the ''index'' and not the value, as the intuition would suggest. See examples below.<br>
</blockquote>


=Iterating over Indices=
<font color=darkkhaki>


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


=Iterating over Values=
* <tt>[[Go for#for_Controlled_by_a_.22range.22_Clause|for]]</tt>
* [[Go Channels|Channels]]


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


=Iterating over Indices and Values=
<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.


<pre>
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
var a [5]int
: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.<br>
for i, value := range a {
</blockquote>
  // i is the index
  // 'value' is the value
}
</pre>


=<tt>range</tt> and Channels=
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
:Note 2: <tt>range</tt> 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.<br>
</blockquote>


<tt>range</tt> can be used to read 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. When the channel is closed, the <tt>for</tt> loop exists:
==Iterating over Key/Values in a Map==


<pre>
<pre>
var c chan string
m := map[string]string {
    "k1": "v1",
    "k2": "v2",
    "k3": "v3",
}


for m := range c {
for key, value := range m {
  // do something with the message
    //  
}
}
</pre>
</pre>
If the channel is nil, the range expression blocks forever.

Latest revision as of 00:00, 27 August 2024

Internal

Overview

range can be used to iterated through channels.

Iterating through Arrays

Iterating though Arrays

Iterating through Slices

Iterating through Slices

Iterating over Map Keys and Values

Iterating over Map Keys and Values

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 Key/Values in a Map

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

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