summaryrefslogtreecommitdiff
path: root/src/fmt/print.go
diff options
context:
space:
mode:
authorMartin Möhrmann <moehrmann@google.com>2018-08-19 07:50:39 +0200
committerMartin Möhrmann <moehrmann@google.com>2018-08-22 21:32:42 +0000
commit773e89464560833711c2554420d1a1550e0e8ff3 (patch)
tree96596d3345c4f0d2ddf9a3eb28faca368afbcad1 /src/fmt/print.go
parent34c58fe184fa73ecff0e7142fcf567b1a9abc01b (diff)
downloadgo-git-773e89464560833711c2554420d1a1550e0e8ff3.tar.gz
fmt: print values for map keys with non-reflexive equality
Previously fmt would first obtain a list of map keys and then look up the value for each key. Since NaNs can be map keys but cannot be fetched directly, the lookup would fail and return a zero reflect.Value, which formats as <nil>. golang.org/cl/33572 added a map iterator to the reflect package that is used in this CL to retrieve the key and value from the map and prints the correct value even for keys that are not equal to themselves. Fixes #14427 Change-Id: I9e1522959760b3de8b7ecf7a6e67cd603339632a Reviewed-on: https://go-review.googlesource.com/129777 Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/fmt/print.go')
-rw-r--r--src/fmt/print.go8
1 files changed, 4 insertions, 4 deletions
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('}')