Repository navigation
fix(fmt): format maps and function types as Go does - #548
Merged
Merged
Conversation
fmt formatted a map as {k:v ...} in insertion order, and %T of a function value as function.
A map now formats as map[k:v ...], with its entries sorted by key as Go's fmt sorts them, and in Go syntax for %#v. The Go type string of a type info now spells function types and the empty interface, which %T gives for a function value, and %#v for a nil function.
Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fmt formatted a map as {k:v ...} in insertion order, where Go writes map[k:v ...] with the entries sorted by key, and %T of a function value gave "function".
A map now formats as map[k:v ...], with its key and element types passed down for formatting the entries, and its entries sorted as Go's internal/fmtsort sorts them: numbers by value with NaN first, strings lexically, false before true, arrays and structs element by element. %#v writes the map in Go syntax, map[string]int{"a":1, "b":2}.
typeInfoGoString now spells function types, func(int, ...string) (bool, error), and the empty interface, so %T of a function value gives its signature, and %#v of a nil func its type.
fmt_map_sorted and fmt_type_func check these against Go, and the fmt unit test of map formatting now expects Go's output.