1
0
mirror of https://github.com/golang/go synced 2024-11-26 00:57:56 -07:00

print the value using (in effect) %v when Printf is given mismatched args for its format

Printf("%s", 2) gives %s(int=2)

R=rsc
DELTA=12  (10 added, 0 deleted, 2 changed)
OCL=34042
CL=34044
This commit is contained in:
Rob Pike 2009-08-28 13:02:34 -07:00
parent 8fdc5b6041
commit c6540d31f6
2 changed files with 12 additions and 2 deletions

View File

@ -85,6 +85,10 @@ var fmttests = []fmtTest{
fmtTest{ "%v", &array, "&[1 2 3 4 5]" },
fmtTest{ "%v", &iarray, "&[1 hello 2.5 <nil>]" },
// erroneous formats
fmtTest{ "", 2, "?(extra int=2)" },
fmtTest{ "%d", "hello", "%d(string=hello)%" },
// old test/fmt_test.go
fmtTest{ "%d", 1234, "1234" },
fmtTest{ "%d", -1234, "-1234" },

View File

@ -700,14 +700,20 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) {
default:
badtype:
s = "%" + string(c) + "(" + field.Type().String() + ")%";
s = "%" + string(c) + "(" + field.Type().String() + "=";
p.addstr(s);
p.printField(field);
s= ")%";
}
p.addstr(s);
}
if fieldnum < v.NumField() {
p.addstr("?(extra ");
for ; fieldnum < v.NumField(); fieldnum++ {
p.addstr(getField(v, fieldnum).Type().String());
field := getField(v, fieldnum);
p.addstr(field.Type().String());
p.addstr("=");
p.printField(field);
if fieldnum + 1 < v.NumField() {
p.addstr(", ");
}