spec: document special cases for floating-point maps #24049
Labels
Documentation
Issues describing a change to documentation.
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
In #20660 (comment), I noted that there are undocumented special cases in the handling of maps with floating-point keys.
Regardless of what happens with that proposal for Go 2, we should fix the Go 1 spec to properly document those cases.
In Map types, add:
Elements may be added during execution using assignments and retrieved with index expressions and range clauses; they may be removed with the
delete
built-in function.In Index expressions, add:
For a of map type
M
:x
's type must be assignable to the key type ofM
x
,a[x]
is the map element with keyx
and the type ofa[x]
is the element type ofM
nil
or does not contain such an entry and the index expression is not a left-hand side operand of an assignment,a[x]
is the zero value for the element type ofM
In Assignments, add:
The assignment proceeds in two phases. First, the operands of index expressions and pointer indirections (including implicit pointer indirections in selectors) on the left and the expressions on the right are all evaluated in the usual order. Second, the assignments are carried out in left-to-right order.
If the left operand of an assignment is an index expression on a map that does not contain a key equal to the given key, the assignment creates a new entry in the map. If the map is
nil
, a run-time panic occurs.In For statements with
range
clause, add:[…]
For a map value, the "range" clause iterates over successive entries of the map.
On successive iterations, the index value will be the key, and the second value will be the element associated with that key. If the key is or contains a NaN value, it may be associated with multiple entries; the elements of those entries can only be retrieved using "range" clauses (not index expressions).
The iteration order over maps is not specified and is not guaranteed to be the same from one iteration to the next. If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced. If a map entry is created during iteration, that entry may be produced during the iteration or may be skipped. The choice may vary for each entry created and from one iteration to the next. If the map is nil, the number of iterations is 0.
In Deletion of map elements, add:
The built-in function delete removes the element with key equal to
k
from a mapm
.The text was updated successfully, but these errors were encountered: