diff --git a/doc/go_spec.html b/doc/go_spec.html index ea7a75c497..8e1b45ab96 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,5 +1,5 @@ - + @@ -3572,8 +3571,8 @@ f(x+y)
The "++" and "--" statements increment or decrement their operands
by the untyped constant 1.
-As with an assignment, the operand must be a variable, pointer indirection,
-field selector or index expression.
+As with an assignment, the operand must be addressable
+or a map index expression.
@@ -3591,6 +3590,7 @@ x++ x += 1 x-- x -= 1+
@@ -3949,59 +3949,81 @@ for { S() } is the same as for true { S() }
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 first assigns the current index or key to an iteration
-variable - or the current (index, element) or (key, value) pair to a pair
-of iteration variables - and then executes the block.
+or values received on a channel. For each entry it assigns iteration values
+to corresponding iteration variables and then executes the block.
-RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
+RangeClause = Expression [ "," Expression ] ( "=" | ":=" ) "range" Expression .
-The type of the right-hand expression in the "range" clause must be an
-array, slice, string or map, or a pointer to an array;
-or it may be a channel.
-Except for channels,
-the identifier list must contain one or two expressions
-(as in assignments, these must be a
-variable, pointer indirection, field selector, or index expression)
-denoting the
-iteration variables. On each iteration,
-the first variable is set to the string, array or slice index or
-map key, and the second variable, if present, is set to the corresponding
-string or array element or map value.
-The types of the array or slice index (always int)
-and element, or of the map key and value respectively,
-must be assignable to
-the type of the iteration variables. The expression on the right hand
-side is evaluated once before beginning the loop. At each iteration
-of the loop, the values produced by the range clause are assigned to
-the left hand side as in an assignment
-statement. Function calls on the left hand side will be evaluated
-exactly once per iteration.
-
+The expression on the right in the "range" clause is called the range expression,
+which may be an array, pointer to an array, slice, string, map, or channel.
+As with an assignment, the operands on the left must be
+addressable or map index expressions; they
+denote the iteration variables. If the range expression is a channel, only
+one iteration variable is permitted, otherwise there may be one or two.
-For a value of a string type, the "range" clause iterates over the Unicode code points
-in the string. On successive iterations, the index variable will be the
-index of the first byte of successive UTF-8-encoded code points in the string, and
-the second variable, of type int, will be the value of
+
+
+The range expression is evaluated once before beginning the loop.
+Function calls on the left are evaluated once per iteration.
+For each iteration, iteration values are produced as follows:
+
+
+
+Range expression 1st value 2nd value (if 2nd variable is present)
+
+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 int
+map m map[K]V key k K m[k] V
+channel c chan E element e E
+
+
+
+-
+For an array or slice value, the index iteration values are produced in
+increasing order, starting at element index 0.
+
+
+-
+For a string value, the "range" clause iterates over the Unicode code points
+in the string starting at byte index 0. On successive iterations, the index value will be the
+index of the first byte of successive UTF-8-encoded code points in the string,
+and the second value, of type
int, will be the value of
the corresponding code point. If the iteration encounters an invalid
-UTF-8 sequence, the second variable will be 0xFFFD,
+UTF-8 sequence, the second value will be 0xFFFD,
the Unicode replacement character, and the next iteration will advance
a single byte in the string.
-
+
+
+-
+The iteration order over maps is not specified.
+If map entries that have not yet been reached are deleted during iteration,
+the corresponding iteration values will not be produced. If map entries are
+inserted during iteration, the behavior is implementation-dependent, but the
+iteration values for each entry will be produced at most once.
+
+
+-
+For channels, the iteration values produced are the successive values sent on
+the channel until the channel is closed; it does not produce the zero value sent
+before the channel is closed
+(§
close and closed).
+
+
-For channels, the identifier list must contain one identifier.
-The iteration receives values sent on the channel until the channel is closed;
-it does not process the zero value sent before the channel is closed.
+The iteration values are assigned to the respective
+iteration variables as in an assignment statement.
+
-The iteration variables may be declared by the "range" clause (":="), in which
-case their scope ends at the end of the "for" statement (§Declarations and
-scope rules). In this case their types are set to
-int and the array element type, or the map key and value types, respectively.
+The iteration variables may be declared by the "range" clause (:=).
+In this case their types are set to the types of the respective iteration values
+and their scope ends at the end of the "for"
+statement; they are re-used in each iteration.
If the iteration variables are declared outside the "for" statement,
after execution their values will be those of the last iteration.
@@ -4026,11 +4048,6 @@ for key, val = range m {
// val == map[key]
--If map entries that have not yet been processed are deleted during iteration, -they will not be processed. If map entries are inserted during iteration, the -behavior is implementation-dependent, but each entry will be processed at most once. -