]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: fix go syntax formatting of []byte(nil)
authorShenghou Ma <minux.ma@gmail.com>
Thu, 3 Apr 2014 20:11:03 +0000 (16:11 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 3 Apr 2014 20:11:03 +0000 (16:11 -0400)
Fixes #7639.

LGTM=rsc
R=r, adg, rsc
CC=golang-codereviews
https://golang.org/cl/81240043

src/pkg/fmt/fmt_test.go
src/pkg/fmt/print.go

index c7a09dedd9f8f0d81aaafdcf450947dd129c5092..3d6ac76a35898ba23f3dc3a0bf0eebc80031398d 100644 (file)
@@ -403,6 +403,8 @@ var fmtTests = []struct {
        {"%#v", "foo", `"foo"`},
        {"%#v", barray, `[5]fmt_test.renamedUint8{0x1, 0x2, 0x3, 0x4, 0x5}`},
        {"%#v", bslice, `[]fmt_test.renamedUint8{0x1, 0x2, 0x3, 0x4, 0x5}`},
+       {"%#v", []byte(nil), "[]byte(nil)"},
+       {"%#v", []int32(nil), "[]int32(nil)"},
 
        // slices with other formats
        {"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`},
index c56d5b94010d23b9ff6a7ef03465e3606f7d4b14..302661f4c85fc46285f7e1ccc5e490b5ea1f3355 100644 (file)
@@ -523,6 +523,15 @@ func (p *pp) fmtString(v string, verb rune, goSyntax bool) {
 func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, typ reflect.Type, depth int) {
        if verb == 'v' || verb == 'd' {
                if goSyntax {
+                       if v == nil {
+                               if typ == nil {
+                                       p.buf.WriteString("[]byte(nil)")
+                               } else {
+                                       p.buf.WriteString(typ.String())
+                                       p.buf.Write(nilParenBytes)
+                               }
+                               return
+                       }
                        if typ == nil {
                                p.buf.Write(bytesBytes)
                        } else {