1
0
mirror of https://github.com/golang/go synced 2024-11-12 05:30:21 -07:00

fmt: fix go syntax formatting of []byte(nil)

Fixes #7639.

LGTM=rsc
R=r, adg, rsc
CC=golang-codereviews
https://golang.org/cl/81240043
This commit is contained in:
Shenghou Ma 2014-04-03 16:11:03 -04:00 committed by Russ Cox
parent 5fb39cc6a2
commit c274ff6761
2 changed files with 11 additions and 0 deletions

View 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]`},

View 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 {