src/pkg/fmt/fmt_test.go | 5 +++++ src/pkg/fmt/print.go | 9 +++++++-- diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index ecceeb09cec65b4ba55ec7c8f4738a950658ccb3..9fdf0ddb36408f771ae745ab2bc88d9618c800a6 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -229,6 +229,11 @@ fmtTest{"%#v", TestFmtInterface, "(func(*testing.T))(PTR)"}, fmtTest{"%#v", make(chan int), "(chan int)(PTR)"}, fmtTest{"%#v", uint64(1<<64 - 1), "0xffffffffffffffff"}, fmtTest{"%#v", 1000000000, "1000000000"}, + + // erroneous things + fmtTest{"%d", "hello", "%d(string=hello)"}, + fmtTest{"no args", "hello", "no args?(extra string=hello)"}, + fmtTest{"%s", nil, "%s()"}, } func TestSprintf(t *testing.T) { diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go index de64179cc876f69e104f799287b80ee20a2291e6..e4840b940bb688c7de582a767a58c82c691c5091 100644 --- a/src/pkg/fmt/print.go +++ b/src/pkg/fmt/print.go @@ -378,6 +378,9 @@ return } func getString(a interface{}) (val string, ok bool) { + if a == nil { + return "", ok + } // Is it a regular string or []byte type? switch s := a.(type) { case string: @@ -941,8 +944,10 @@ badtype: p.buf.WriteByte('%') p.add(c) p.buf.WriteByte('(') - p.buf.WriteString(reflect.Typeof(field).String()) - p.buf.WriteByte('=') + if field != nil { + p.buf.WriteString(reflect.Typeof(field).String()) + p.buf.WriteByte('=') + } p.printField(field, false, false, 0) p.buf.WriteByte(')') }