From e5ef4846911d91eed4fdea27e6bfeb5733d829a6 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 31 Oct 2023 15:38:35 -0700 Subject: [PATCH] spec: document range over integer expression This CL is partly based on CL 510535. For #61405. Change-Id: Ic94f6726f9eb34313f11bec7b651921d7e5c18d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/538859 Reviewed-by: Cherry Mui TryBot-Bypass: Robert Griesemer Auto-Submit: Robert Griesemer Reviewed-by: Robert Griesemer --- doc/go_spec.html | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 38130a3cc9..18f88d5ead 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -6552,8 +6552,9 @@ 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 assigns iteration values +iterates through all entries of an array, slice, string or map, values received on +a channel, or integer values from zero to an upper limit. +For each entry it assigns iteration values to corresponding iteration variables if present and then executes the block.

@@ -6564,12 +6565,12 @@ RangeClause = [ ExpressionList "=" | IdentifierList ":=" ] "range" Expression .

The expression on the right in the "range" clause is called the range expression, its core type must be -an array, pointer to an array, slice, string, map, or channel permitting -receive operations. +an array, pointer to an array, slice, string, map, channel permitting +receive operations, or an integer. As with an assignment, if present the operands on the left must be addressable or map index expressions; they -denote the iteration variables. If the range expression is a channel, at most -one iteration variable is permitted, otherwise there may be up to two. +denote the iteration variables. If the range expression is a channel or integer, +at most one iteration variable is permitted, otherwise there may be up to two. If the last iteration variable is the blank identifier, the range clause is equivalent to the same clause without that identifier.

@@ -6594,6 +6595,7 @@ 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 I value i I
    @@ -6632,6 +6634,12 @@ For channels, the iteration values produced are the successive values sent on the channel until the channel is closed. If the channel is nil, the range expression blocks forever. + +
  1. +For an integer value n, the iteration values 0 through n-1 +are produced in increasing order, with the same type as n. +If n <= 0, the loop does not run any iterations. +

@@ -6684,6 +6692,12 @@ for w := range ch { // empty a channel for range ch {} + +// call f(0), f(1), ... f(9) +for i := range 10 { + // type of i is int (default type for untyped constant 10) + f(i) +} -- 2.44.0