From cc1d7afb247677fd9a1f75d11587af00d65e231d Mon Sep 17 00:00:00 2001
From: Robert Griesemer
Composite literals construct new values for structs, arrays, slices, and maps
each time they are evaluated.
-They consist of the type of the literal followed by a brace-bound list of elements.
+They consist of the type of the literal followed by a (possibly empty)
+brace-bound list of elements.
Each element may optionally be preceded by a corresponding key.
T;
-there is no additional conversion.
-The key is interpreted as a field name for struct literals,
-an index for array and slice literals, and a key for map literals.
-For map literals, all elements must have a key. It is an error
-to specify multiple elements with the same field name or
-constant key value. For non-constant map keys, see the section on
-evaluation order.
-For struct literals the following rules apply: +The types of the elements and keys must be assignable +to the respective field, element, and key types of the LiteralType; +there is no additional conversion. +The key is interpreted as a field name for struct literals, +an index for array and slice literals, and a key for map literals. +It is an error to specify multiple elements with the same field name +or constant key value. +A literal may omit the element list; such a literal evaluates +to the zero value for its type. +
+ ++A parsing ambiguity arises when a composite literal using the +TypeName form of the LiteralType appears as an operand between the +keyword and the opening brace of the block +of an "if", "for", or "switch" statement, and the composite literal +is not enclosed in parentheses, square brackets, or curly braces. +In this rare case, the opening brace of the literal is erroneously parsed +as the one introducing the block of statements. To resolve the ambiguity, +the composite literal must appear within parentheses. +
+ +
+if x == (T{a,b,c}[i]) { … }
+if (x == T{a,b,c}[i]) { … }
+
+
++For struct literals without keys, the element list must contain an element +for each struct field in the order in which the fields are declared. +
+ ++For struct literals with keys the following rules apply:
For array and slice literals the following rules apply:
@@ -3295,6 +3316,16 @@ tmp := [n]T{x1, x2, … xn} tmp[0 : n] ++For map literals, each element must have a key. +For non-constant map keys, see the section on +evaluation order. +
+ +
Within a composite literal of array, slice, or map type T,
elements or map keys that are themselves composite literals may elide the respective
@@ -3315,22 +3346,6 @@ type PPoint *Point
[2]PPoint{{1.5, -3.5}, {}} // same as [2]PPoint{PPoint(&Point{1.5, -3.5}), PPoint(&Point{})}
-
-A parsing ambiguity arises when a composite literal using the -TypeName form of the LiteralType appears as an operand between the -keyword and the opening brace of the block -of an "if", "for", or "switch" statement, and the composite literal -is not enclosed in parentheses, square brackets, or curly braces. -In this rare case, the opening brace of the literal is erroneously parsed -as the one introducing the block of statements. To resolve the ambiguity, -the composite literal must appear within parentheses. -
- -
-if x == (T{a,b,c}[i]) { … }
-if (x == T{a,b,c}[i]) { … }
-
-
Examples of valid array, slice, and map literals: