mirror of
https://github.com/golang/go
synced 2024-11-22 06:24:38 -07:00
- added language for range statement
SVN=111200
This commit is contained in:
parent
328df636c5
commit
266b9d49bf
40
doc/go_spec
40
doc/go_spec
@ -1,18 +1,13 @@
|
|||||||
The Go Annotated Specification
|
The Go Annotated Specification
|
||||||
|
|
||||||
This document supersedes all previous Go spec attempts. The intent is
|
This document supersedes all previous Go spec attempts. The intent
|
||||||
to make this a reference for syntax and semantics. It is annotated
|
is to make this a reference for syntax and semantics. It is annotated
|
||||||
with additional information not strictly belonging into a language
|
with additional information not strictly belonging into a language
|
||||||
spec.
|
spec.
|
||||||
|
|
||||||
|
|
||||||
Open questions
|
Open questions
|
||||||
|
|
||||||
- how to do map iteration? should be symmetric to array iteration
|
|
||||||
for k in m { ... }
|
|
||||||
for k:v in m { ... }
|
|
||||||
for :v in m { ... }
|
|
||||||
|
|
||||||
- how to delete from a map
|
- how to delete from a map
|
||||||
|
|
||||||
- how to test for map membership (we may want an 'atomic install'? m[i] ?= x; )
|
- how to test for map membership (we may want an 'atomic install'? m[i] ?= x; )
|
||||||
@ -710,6 +705,7 @@ PointerType = '*' Type.
|
|||||||
|
|
||||||
- We do not allow pointer arithmetic of any kind.
|
- We do not allow pointer arithmetic of any kind.
|
||||||
|
|
||||||
|
|
||||||
Interface types
|
Interface types
|
||||||
|
|
||||||
- TBD: This needs to be much more precise. For now we understand what it means.
|
- TBD: This needs to be much more precise. For now we understand what it means.
|
||||||
@ -1013,9 +1009,10 @@ func (p *T) foo (a, b int, z float) bool;
|
|||||||
|
|
||||||
Statements
|
Statements
|
||||||
|
|
||||||
Statement = EmptyStat | Assignment | CompoundStat | Declaration |
|
Statement =
|
||||||
|
EmptyStat | Assignment | CompoundStat | Declaration |
|
||||||
ExpressionStat | IncDecStat | IfStat | WhileStat | ForStat |
|
ExpressionStat | IncDecStat | IfStat | WhileStat | ForStat |
|
||||||
ReturnStat .
|
RangeStat | ReturnStat .
|
||||||
|
|
||||||
|
|
||||||
Empty statements
|
Empty statements
|
||||||
@ -1094,6 +1091,31 @@ ForStat = 'for' ...
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Range statements
|
||||||
|
|
||||||
|
Range statements denote iteration over the contents of arrays and maps.
|
||||||
|
|
||||||
|
RangeStat = 'range' IdentifierList ':=' RangeExpression Block .
|
||||||
|
RangeExpression = Expression .
|
||||||
|
|
||||||
|
A range expression must evaluate to an array, map or string. The identifier list must contain
|
||||||
|
either one or two identifiers. If the range expression is a map, a single identifier is declared
|
||||||
|
to range over the keys of the map; two identifiers range over the keys and corresponding
|
||||||
|
values. For arrays and strings, the behavior is analogous for integer indices (the keys) and array
|
||||||
|
elements (the values).
|
||||||
|
|
||||||
|
a := [ 1, 2, 3];
|
||||||
|
m := [ "fo" : 2, "foo" : 3, "fooo" : 4 ]
|
||||||
|
|
||||||
|
range i := a {
|
||||||
|
f(a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
range k, v := m {
|
||||||
|
assert(len(k) == v);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Return statements
|
Return statements
|
||||||
|
|
||||||
ReturnStat = 'return' [ ExpressionList ] .
|
ReturnStat = 'return' [ ExpressionList ] .
|
||||||
|
Loading…
Reference in New Issue
Block a user