]> Cypherpunks repositories - gostls13.git/commitdiff
Expand section on compound literals.
authorRob Pike <r@golang.org>
Tue, 22 Jul 2008 22:23:06 +0000 (15:23 -0700)
committerRob Pike <r@golang.org>
Tue, 22 Jul 2008 22:23:06 +0000 (15:23 -0700)
R=ken,gri
DELTA=31  (22 added, 0 deleted, 9 changed)
OCL=13351
CL=13362

doc/go_lang.txt

index 2cede5e8d3c5df62ba32eb7348fd424711c113c8..12346510091f1f16280a050c8d67ea9e5567494d 100644 (file)
@@ -678,15 +678,37 @@ structure.
   }
 
 
-Compound Literals
+Composite Literals
 ----
 
-Literals for compound data structures consist of the type of the constant
-followed by a parenthesized expression list.  In effect, they are a
-conversion from expression list to compound value.
+Literals for composite data structures consist of the type of the value
+followed by a parenthesized expression list.  In appearance, they are a
+conversion from expression list to composite value.
 
-TODO: Needs to be updated.
+Structure literals follow this form directly.  Given
 
+       type Rat struct { num, den int };
+       type Num struct { r Rat, f float, s string };
+
+we can write
+
+       pi := Num(Rat(22,7), 3.14159, "pi")
+
+For array literals, if the size is present the constructed array has that many
+elements; trailing elements are given the approprate zero value for that type.
+If it is absent, the size of the array is the number of elements. It is an error
+if a specified size is less than the number of elements in the expression list.
+
+  primes := [6]int(2, 3, 5, 7, 9, 11)
+  weekdays := []string("mon", "tue", "wed", "thu", "fri", "sat", "sun")
+
+Map literals are similar except the elements of the expression list are
+key-value pairs separated by a colon:
+
+  m := map[string]int("good":0, "bad":1, "indifferent": 7)
+
+TODO: helper syntax for nested arrays etc? (avoids repeating types but
+complicates the spec needlessly.)
 
 Pointer types
 ----