1
0
mirror of https://github.com/golang/go synced 2024-11-20 09:24:50 -07:00

fmt: don't check for nil when printing arrays as Go syntax.

Also add array values to printing test suite.
Fixes #2468.

R=golang-dev, r
CC=golang-dev, remy
https://golang.org/cl/5436053
This commit is contained in:
Rémy Oudompheng 2011-11-23 09:04:02 -08:00 committed by Rob Pike
parent 8281f6bd1b
commit 8362ee99b0
2 changed files with 15 additions and 3 deletions

View File

@ -47,8 +47,10 @@ func TestFmtInterface(t *testing.T) {
const b32 uint32 = 1<<32 - 1 const b32 uint32 = 1<<32 - 1
const b64 uint64 = 1<<64 - 1 const b64 uint64 = 1<<64 - 1
var array = []int{1, 2, 3, 4, 5} var array = [5]int{1, 2, 3, 4, 5}
var iarray = []interface{}{1, "hello", 2.5, nil} var iarray = [4]interface{}{1, "hello", 2.5, nil}
var slice = array[:]
var islice = iarray[:]
type A struct { type A struct {
i int i int
@ -327,6 +329,12 @@ var fmttests = []struct {
{"%v", &array, "&[1 2 3 4 5]"}, {"%v", &array, "&[1 2 3 4 5]"},
{"%v", &iarray, "&[1 hello 2.5 <nil>]"}, {"%v", &iarray, "&[1 hello 2.5 <nil>]"},
// slices
{"%v", slice, "[1 2 3 4 5]"},
{"%v", islice, "[1 hello 2.5 <nil>]"},
{"%v", &slice, "&[1 2 3 4 5]"},
{"%v", &islice, "&[1 hello 2.5 <nil>]"},
// complexes with %v // complexes with %v
{"%v", 1 + 2i, "(1+2i)"}, {"%v", 1 + 2i, "(1+2i)"},
{"%v", complex64(1 + 2i), "(1+2i)"}, {"%v", complex64(1 + 2i), "(1+2i)"},
@ -359,6 +367,10 @@ var fmttests = []struct {
{"%#v", SI{}, `fmt_test.SI{I:interface {}(nil)}`}, {"%#v", SI{}, `fmt_test.SI{I:interface {}(nil)}`},
{"%#v", []int(nil), `[]int(nil)`}, {"%#v", []int(nil), `[]int(nil)`},
{"%#v", []int{}, `[]int{}`}, {"%#v", []int{}, `[]int{}`},
{"%#v", array, `[5]int{1, 2, 3, 4, 5}`},
{"%#v", &array, `&[5]int{1, 2, 3, 4, 5}`},
{"%#v", iarray, `[4]interface {}{1, "hello", 2.5, interface {}(nil)}`},
{"%#v", &iarray, `&[4]interface {}{1, "hello", 2.5, interface {}(nil)}`},
{"%#v", map[int]byte(nil), `map[int] uint8(nil)`}, {"%#v", map[int]byte(nil), `map[int] uint8(nil)`},
{"%#v", map[int]byte{}, `map[int] uint8{}`}, {"%#v", map[int]byte{}, `map[int] uint8{}`},

View File

@ -877,7 +877,7 @@ BigSwitch:
} }
if goSyntax { if goSyntax {
p.buf.WriteString(value.Type().String()) p.buf.WriteString(value.Type().String())
if f.IsNil() { if f.Kind() == reflect.Slice && f.IsNil() {
p.buf.WriteString("(nil)") p.buf.WriteString("(nil)")
break break
} }