Skip to content

Commit a2ef54b

Browse files
committed
fmt: adjust formatting of invalid reflect.Value, add more tests
Repeat of CL 8951. Change-Id: I5430e4a9eb5d8b7d0e3963657092bede67439056 Reviewed-on: https://go-review.googlesource.com/9003 Reviewed-by: Rob Pike <[email protected]>
1 parent c8aba85 commit a2ef54b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/fmt/fmt_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,14 @@ var fmtTests = []struct {
686686
// Issue 8965.
687687
{"%v", reflect.ValueOf(A{}).Field(0).String(), "<int Value>"}, // Equivalent to the old way.
688688
{"%v", reflect.ValueOf(A{}).Field(0), "0"}, // Sees inside the field.
689+
690+
// verbs apply to the extracted value too.
691+
{"%s", reflect.ValueOf("hello"), "hello"},
692+
{"%q", reflect.ValueOf("hello"), `"hello"`},
693+
{"%#04x", reflect.ValueOf(256), "0x0100"},
694+
695+
// invalid reflect.Value doesn't crash.
696+
{"%v", reflect.Value{}, "<invalid reflect.Value>"},
689697
}
690698

691699
// zeroFill generates zero-filled strings of the specified width. The length

src/fmt/print.go

+2
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,8 @@ func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasSt
847847
p.value = value
848848
BigSwitch:
849849
switch f := value; f.Kind() {
850+
case reflect.Invalid:
851+
p.buf.WriteString("<invalid reflect.Value>")
850852
case reflect.Bool:
851853
p.fmtBool(f.Bool(), verb)
852854
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:

0 commit comments

Comments
 (0)