1
0
mirror of https://github.com/golang/go synced 2024-10-03 17:21:21 -06:00

fmt: don't panic formatting nil interfaces

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5296044
This commit is contained in:
Gustavo Niemeyer 2011-10-19 18:26:08 -02:00
parent b0ec32db11
commit 526d0818cc
2 changed files with 6 additions and 1 deletions

View File

@ -88,6 +88,10 @@ type S struct {
G G // a struct field that GoStrings G G // a struct field that GoStrings
} }
type SI struct {
I interface{}
}
// A type with a String method with pointer receiver for testing %p // A type with a String method with pointer receiver for testing %p
type P int type P int
@ -352,6 +356,7 @@ var fmttests = []struct {
{"%#v", map[string]int{"a": 1}, `map[string] int{"a":1}`}, {"%#v", map[string]int{"a": 1}, `map[string] int{"a":1}`},
{"%#v", map[string]B{"a": {1, 2}}, `map[string] fmt_test.B{"a":fmt_test.B{I:1, j:2}}`}, {"%#v", map[string]B{"a": {1, 2}}, `map[string] fmt_test.B{"a":fmt_test.B{I:1, j:2}}`},
{"%#v", []string{"a", "b"}, `[]string{"a", "b"}`}, {"%#v", []string{"a", "b"}, `[]string{"a", "b"}`},
{"%#v", SI{}, `fmt_test.SI{I:interface { }(nil)}`},
// slices with other formats // slices with other formats
{"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`}, {"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`},

View File

@ -842,7 +842,7 @@ BigSwitch:
value := f.Elem() value := f.Elem()
if !value.IsValid() { if !value.IsValid() {
if goSyntax { if goSyntax {
p.buf.WriteString(value.Type().String()) p.buf.WriteString(f.Type().String())
p.buf.Write(nilParenBytes) p.buf.Write(nilParenBytes)
} else { } else {
p.buf.Write(nilAngleBytes) p.buf.Write(nilAngleBytes)