From 29f1ca528b574528cc8e0ececf934b737c75de7d Mon Sep 17 00:00:00 2001
From: Robert Griesemer A
is an array type,
or for a
of type S
where S
is a slice type:
x
must be an integer value and 0 <= x < len(a)
+ x
must be an integer value and 0 <= x < len(a)
a[x]
is the array element at index x
and the type of
- a[x]
is the element type of A
+ a[x]
is the element type of A
x
is out of range, a run-time exception occurs
@@ -2404,10 +2405,11 @@ For a
of type T
where T
is a string type:
x
must be an integer value and 0 <= x < len(a)
+ x
must be an integer value and 0 <= x < len(a)
a[x]
is the byte at index x
and the type of
- a[x]
is byte
+ a[x]
is byte
a[x]
may not be assigned to
+ x
is out of range, a run-time exception occurs
@@ -2415,38 +2417,38 @@ For a
of type M
where M
is a map type:
x
's type must be compatible with the key type of M
- and the map must contain an entry with key x
(but see special forms below)
- a[x]
is the map value with key x
- and the type of a[x]
is the value type of M
+ x
's type must be
+ assignment compatible
+ with the key type of M
x
,
+ a[x]
is the map value with key x
+ and the type of a[x]
is the value type of M
a[x]
is the zero value
+ for the value type of M
-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.