From 526d0818cca60a021e8c3c5ca34f7ed7d43f61ae Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Wed, 19 Oct 2011 18:26:08 -0200 Subject: [PATCH] fmt: don't panic formatting nil interfaces R=golang-dev, r CC=golang-dev https://golang.org/cl/5296044 --- src/pkg/fmt/fmt_test.go | 5 +++++ src/pkg/fmt/print.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index 030ad6187f..38280d61f6 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -88,6 +88,10 @@ type S struct { G G // a struct field that GoStrings } +type SI struct { + I interface{} +} + // A type with a String method with pointer receiver for testing %p type P int @@ -352,6 +356,7 @@ var fmttests = []struct { {"%#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", []string{"a", "b"}, `[]string{"a", "b"}`}, + {"%#v", SI{}, `fmt_test.SI{I:interface { }(nil)}`}, // slices with other formats {"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`}, diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go index 7721e72411..ba50b93283 100644 --- a/src/pkg/fmt/print.go +++ b/src/pkg/fmt/print.go @@ -842,7 +842,7 @@ BigSwitch: value := f.Elem() if !value.IsValid() { if goSyntax { - p.buf.WriteString(value.Type().String()) + p.buf.WriteString(f.Type().String()) p.buf.Write(nilParenBytes) } else { p.buf.Write(nilAngleBytes) -- 2.50.0