From: Robert Griesemer
-Range expression 1st value 2nd value +Range expression 1st value 2nd value -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, <-chan E element e E -integer n integer type value i see below +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, <-chan E element e E +integer value n integer type, or untyped int value i see below
nil
, the range expression blocks forever.
n
, the iteration values 0 through n-1
+For an integer value n
, where n
is of integer type
+or an untyped integer constant, the iteration values 0 through n-1
are produced in increasing order.
+If n
is of integer type, the iteration values have that same type.
+Otherwise, the type of n
is determined as if it were assigned to the
+iteration variable.
+Specifically:
+if the iteration variable is preexisting, the type of the iteration values is the type of the iteration
+variable, which must be of integer type.
+Otherwise, if the iteration variable is declared by the "range" clause or is absent,
+the type of the iteration values is the default type for n
.
If n
<= 0, the loop does not run any iterations.
n
,
-the variable has the same type as if it was
-declared with initialization
-expression n
.
-Otherwise, the variables have the types of their respective iteration values.
+The variables have the types of their respective iteration values.
@@ -6728,9 +6733,6 @@ If the iteration variables are not explicitly declared by the "range" clause,
they must be preexisting.
In this case, the iteration values are assigned to the respective variables
as in an assignment statement.
-If the range expression is a (possibly untyped) integer expression n
,
-n
too must be assignable to the iteration variable;
-if there is no iteration variable, n
must be assignable to int
.
@@ -6778,6 +6780,10 @@ for i := range 10 { var u uint8 for u = range 256 { } + +// invalid: 1e3 is a floating-point constant +for range 1e3 { +}