Skip to content

Commit 61e02ee

Browse files
committed
spec: clarify when range x does not evaluate x
Fixes #4644. R=r, adonovan CC=golang-dev https://golang.org/cl/7307083
1 parent 318309a commit 61e02ee

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

doc/go_spec.html

+11-5
Original file line numberDiff line numberDiff line change
@@ -4377,9 +4377,15 @@ <h3 id="For_statements">For statements</h3>
43774377
</p>
43784378

43794379
<p>
4380-
The range expression is evaluated once before beginning the loop
4381-
except if the expression is an array, in which case, depending on
4382-
the expression, it might not be evaluated (see below).
4380+
The range expression is evaluated once before beginning the loop,
4381+
with one exception. If the range expression is an array or a pointer to an array
4382+
and only the first iteration value is present, only the range expression's
4383+
length is evaluated; if that length is constant by definition
4384+
(see §<a href="#Length_and_capacity">Length and capacity</a>),
4385+
the range expression itself will not be evaluated.
4386+
</p>
4387+
4388+
<p>
43834389
Function calls on the left are evaluated once per iteration.
43844390
For each iteration, iteration values are produced as follows:
43854391
</p>
@@ -4396,8 +4402,8 @@ <h3 id="For_statements">For statements</h3>
43964402
<ol>
43974403
<li>
43984404
For an array, pointer to array, or slice value <code>a</code>, the index iteration
4399-
values are produced in increasing order, starting at element index 0. As a special
4400-
case, if only the first iteration variable is present, the range loop produces
4405+
values are produced in increasing order, starting at element index 0.
4406+
If only the first iteration variable is present, the range loop produces
44014407
iteration values from 0 up to <code>len(a)</code> and does not index into the array
44024408
or slice itself. For a <code>nil</code> slice, the number of iterations is 0.
44034409
</li>

0 commit comments

Comments
 (0)