From 61e02ee901e361586291087a986680ee98da6da0 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 15 Feb 2013 14:39:28 -0500 Subject: [PATCH] spec: clarify when range x does not evaluate x Fixes #4644. R=r, adonovan CC=golang-dev https://golang.org/cl/7307083 --- doc/go_spec.html | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 5f84a29645..66294c7a5a 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -4377,9 +4377,15 @@ the range clause is equivalent to the same clause with only the first variable p

-The range expression is evaluated once before beginning the loop -except if the expression is an array, in which case, depending on -the expression, it might not be evaluated (see below). +The range expression is evaluated once before beginning the loop, +with one exception. If the range expression is an array or a pointer to an array +and only the first iteration value is present, only the range expression's +length is evaluated; if that length is constant by definition +(see §Length and capacity), +the range expression itself will not be evaluated. +

+ +

Function calls on the left are evaluated once per iteration. For each iteration, iteration values are produced as follows:

@@ -4396,8 +4402,8 @@ channel c chan E, <-chan E element e E
  1. For an array, pointer to array, or slice value a, the index iteration -values are produced in increasing order, starting at element index 0. As a special -case, if only the first iteration variable is present, the range loop produces +values are produced in increasing order, starting at element index 0. +If only the first iteration variable is present, the range loop produces iteration values from 0 up to len(a) and does not index into the array or slice itself. For a nil slice, the number of iterations is 0.
  2. -- 2.48.1