]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: update formatting example for maps
authorRob Pike <r@golang.org>
Mon, 26 Nov 2018 04:58:56 +0000 (15:58 +1100)
committerRob Pike <r@golang.org>
Mon, 26 Nov 2018 05:08:44 +0000 (05:08 +0000)
Now that maps are printed in deterministic order, the map example
can have multiple keys without breaking the build.

Change-Id: Iccec0cd76a3d41c75d8d4eb768ec0ac09ad9f2ad
Reviewed-on: https://go-review.googlesource.com/c/151218
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>

src/fmt/example_test.go

index c2851759769b1ca948e334c04c47998eaed4c7fd..56ce47f83639aed139434478bdc3b1b1958d3a0a 100644 (file)
@@ -290,15 +290,14 @@ func Example_formats() {
 
        // Maps formatted with %v show keys and values in their default formats.
        // The %#v form (the # is called a "flag" in this context) shows the map in
-       // the Go source format.
+       // the Go source format. Maps are printed in a consistent order, sorted
+       // by the values of the keys.
        isLegume := map[string]bool{
-               "peanut": true,
-               // TODO: Include this line when maps are printed in deterministic order.
-               // See Issue #21095
-               // "dachshund": false,
+               "peanut":    true,
+               "dachshund": false,
        }
        fmt.Printf("%v %#v\n", isLegume, isLegume)
-       // Result: map[peanut:true] map[string]bool{"peanut":true}
+       // Result: map[dachshund:false peanut:true] map[string]bool{"dachshund":false, "peanut":true}
 
        // Structs formatted with %v show field values in their default formats.
        // The %+v form shows the fields by name, while %#v formats the struct in
@@ -356,7 +355,7 @@ func Example_formats() {
        // (110.7+22.5i) (110.7+22.5i) (110.70+22.50i) (1.11e+02+2.25e+01i)
        // 128512 128512 ðŸ˜€ '😀' U+1F600 U+1F600 '😀'
        // foo "bar" foo "bar" "foo \"bar\"" `foo "bar"`
-       // map[peanut:true] map[string]bool{"peanut":true}
+       // map[dachshund:false peanut:true] map[string]bool{"dachshund":false, "peanut":true}
        // {Kim 22} {Name:Kim Age:22} struct { Name string; Age int }{Name:"Kim", Age:22}
        // &{Kim 22} 0x0
        // [Katano Kobayashi Kurosawa Miyazaki Ozu] ["Katano" "Kobayashi" "Kurosawa" "Miyazaki" "Ozu"]