From 29f1ca528b574528cc8e0ececf934b737c75de7d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 23 Mar 2010 14:01:51 -0700 Subject: [PATCH] Go spec: map indexing never raises a runtime exception. Also: Actual map key must be assignment-compatible with formal map key type. Fixes #357. R=r, iant, rsc, ken2 CC=golang-dev https://golang.org/cl/673042 --- doc/go_spec.html | 53 +++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 83d9f90e412..2262d7d99e9 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -2394,9 +2394,10 @@ where A is an array type, or for a of type S where S is a slice type:

@@ -2404,10 +2405,11 @@ For a of type T where T is a string type:

@@ -2415,38 +2417,38 @@ For a of type M where M is a map type:

-Otherwise a[x] is illegal. If the index or key is out of range evaluating -an otherwise legal index expression, a run-time exception occurs. +Otherwise a[x] is illegal.

-However, if an index expression on a map a of type map[K] V -is used in an assignment or initialization of the form +An index expression on a map a of type map[K]V +may be used in an assignment or initialization of the special form

-r, ok = a[x]
-r, ok := a[x]
-var r, ok = a[x]
+v, ok = a[x]
+v, ok := a[x]
+var v, ok = a[x]
 

-the result of the index expression is a pair of values with types -(V, bool). -If the key is present in the map, -the expression returns the pair (a[x], true); -otherwise it returns (Z, false) where Z is -the zero value for V. -No run-time exception occurs in this case. -The index expression in this construct thus acts like a function call -returning a value and a boolean indicating success. (ยงAssignments) +where the result of the index expression is a pair of values with types +(V, bool). In this form, the value of ok is +true if the key x is present in the map, and +false otherwise. The value of v is the value +a[x] as in the single-result form.

@@ -2454,7 +2456,7 @@ Similarly, if an assignment to a map has the special form

-a[x] = r, ok
+a[x] = v, ok
 

@@ -2464,6 +2466,7 @@ the entry for key x is deleted from the map; if a regular assignment to an element of the map.

+

Slices