summaryrefslogtreecommitdiff
path: root/src/fmt
diff options
context:
space:
mode:
Diffstat (limited to 'src/fmt')
-rw-r--r--src/fmt/fmt_test.go11
-rw-r--r--src/fmt/print.go8
2 files changed, 7 insertions, 12 deletions
diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go
index 08e46b4e93..edfd1ee824 100644
--- a/src/fmt/fmt_test.go
+++ b/src/fmt/fmt_test.go
@@ -861,13 +861,8 @@ var fmtTests = []struct {
// Extra argument errors should format without flags set.
{"%010.2", "12345", "%!(NOVERB)%!(EXTRA string=12345)"},
- // The "<nil>" show up because maps are printed by
- // first obtaining a list of keys and then looking up
- // each key. Since NaNs can be map keys but cannot
- // be fetched directly, the lookup fails and returns a
- // zero reflect.Value, which formats as <nil>.
- // This test is just to check that it shows the two NaNs at all.
- {"%v", map[float64]int{NaN: 1, NaN: 2}, "map[NaN:<nil> NaN:<nil>]"},
+ // Test that maps with non-reflexive keys print all keys and values.
+ {"%v", map[float64]int{NaN: 1, NaN: 1}, "map[NaN:1 NaN:1]"},
// Comparison of padding rules with C printf.
/*
@@ -1033,7 +1028,7 @@ var fmtTests = []struct {
{"%☠", &[]interface{}{I(1), G(2)}, "&[%!☠(fmt_test.I=1) %!☠(fmt_test.G=2)]"},
{"%☠", SI{&[]interface{}{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"},
{"%☠", reflect.Value{}, "<invalid reflect.Value>"},
- {"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(<nil>)]"},
+ {"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(int=1)]"},
}
// zeroFill generates zero-filled strings of the specified width. The length
diff --git a/src/fmt/print.go b/src/fmt/print.go
index f67f805603..c9d694b07d 100644
--- a/src/fmt/print.go
+++ b/src/fmt/print.go
@@ -743,8 +743,8 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
} else {
p.buf.WriteString(mapString)
}
- keys := f.MapKeys()
- for i, key := range keys {
+ iter := f.MapRange()
+ for i := 0; iter.Next(); i++ {
if i > 0 {
if p.fmt.sharpV {
p.buf.WriteString(commaSpaceString)
@@ -752,9 +752,9 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
p.buf.WriteByte(' ')
}
}
- p.printValue(key, verb, depth+1)
+ p.printValue(iter.Key(), verb, depth+1)
p.buf.WriteByte(':')
- p.printValue(f.MapIndex(key), verb, depth+1)
+ p.printValue(iter.Value(), verb, depth+1)
}
if p.fmt.sharpV {
p.buf.WriteByte('}')