aboutsummaryrefslogtreecommitdiff
path: root/doc/go_spec.html
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2023-10-31 15:38:35 -0700
committerRobert Griesemer <gri@google.com>2023-11-02 03:57:56 +0000
commite5ef4846911d91eed4fdea27e6bfeb5733d829a6 (patch)
tree0ed5888373a7e932c0a7db20697f50bfc290fe63 /doc/go_spec.html
parent08b2f1f761a57b6c547eb5c53109237fe0a17b81 (diff)
downloadgo-e5ef4846911d91eed4fdea27e6bfeb5733d829a6.tar.gz
go-e5ef4846911d91eed4fdea27e6bfeb5733d829a6.zip
spec: document range over integer expression
This CL is partly based on CL 510535. For #61405. Change-Id: Ic94f6726f9eb34313f11bec7b651921d7e5c18d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/538859 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Bypass: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
Diffstat (limited to 'doc/go_spec.html')
-rw-r--r--doc/go_spec.html28
1 files changed, 21 insertions, 7 deletions
diff --git a/doc/go_spec.html b/doc/go_spec.html
index 38130a3cc9..18f88d5ead 100644
--- a/doc/go_spec.html
+++ b/doc/go_spec.html
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
- "Subtitle": "Version of Oct 16, 2023",
+ "Subtitle": "Version of Nov 1, 2023",
"Path": "/ref/spec"
}-->
@@ -6552,8 +6552,9 @@ for { S() } is the same as for true { S() }
<p>
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 <i>iteration values</i>
+iterates through all entries of an array, slice, string or map, values received on
+a channel, or integer values from zero to an upper limit.
+For each entry it assigns <i>iteration values</i>
to corresponding <i>iteration variables</i> if present and then executes the block.
</p>
@@ -6564,12 +6565,12 @@ RangeClause = [ ExpressionList "=" | IdentifierList ":=" ] "range" Expression .
<p>
The expression on the right in the "range" clause is called the <i>range expression</i>,
its <a href="#Core_types">core type</a> must be
-an array, pointer to an array, slice, string, map, or channel permitting
-<a href="#Receive_operator">receive operations</a>.
+an array, pointer to an array, slice, string, map, channel permitting
+<a href="#Receive_operator">receive operations</a>, or an integer.
As with an assignment, if present the operands on the left must be
<a href="#Address_operators">addressable</a> or map index expressions; they
-denote the iteration variables. If the range expression is a channel, at most
-one iteration variable is permitted, otherwise there may be up to two.
+denote the iteration variables. If the range expression is a channel or integer,
+at most one iteration variable is permitted, otherwise there may be up to two.
If the last iteration variable is the <a href="#Blank_identifier">blank identifier</a>,
the range clause is equivalent to the same clause without that identifier.
</p>
@@ -6594,6 +6595,7 @@ array or slice a [n]E, *[n]E, or []E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, &lt;-chan E element e E
+integer n integer type I value i I
</pre>
<ol>
@@ -6632,6 +6634,12 @@ For channels, the iteration values produced are the successive values sent on
the channel until the channel is <a href="#Close">closed</a>. If the channel
is <code>nil</code>, the range expression blocks forever.
</li>
+
+<li>
+For an integer value <code>n</code>, the iteration values 0 through <code>n-1</code>
+are produced in increasing order, with the same type as <code>n</code>.
+If <code>n</code> &lt= 0, the loop does not run any iterations.
+</li>
</ol>
<p>
@@ -6684,6 +6692,12 @@ for w := range ch {
// empty a channel
for range ch {}
+
+// call f(0), f(1), ... f(9)
+for i := range 10 {
+ // type of i is int (default type for untyped constant 10)
+ f(i)
+}
</pre>