Commit c274ff67 authored by Shenghou Ma's avatar Shenghou Ma Committed by Russ Cox

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

Fixes #7639.

LGTM=rsc
R=r, adg, rsc
CC=golang-codereviews
https://golang.org/cl/81240043
parent 5fb39cc6
......@@ -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]`},
......
......@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment