From: Robert Griesemer
The "++" and "--" statements increment or decrement their operands
by the untyped constant
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.
-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
-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
+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.
+
+
+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
+
Assignments
@@ -3949,59 +3949,81 @@ for { S() } is the same as for true { S() }
+
+
-RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
-
-
-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.
-int
, will be the value of
+RangeClause = Expression [ "," Expression ] ( "=" | ":=" ) "range" Expression .
+
+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 ++ +
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.
-
+close
and closed
).
+
-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.
-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. -