According to fmt Printing docs <http://golang.org/pkg/fmt/>, %#v prints "a
Go-syntax representation of the value".
Consider the following short Go program:
package main
import "fmt"
func main() {
x := [...]int{1,2, 3, 4, 5}
fmt.Printf("x := %#v\n", x)
}
If you run that .go source file through *gofmt*, you get:
package main
import "fmt"
func main() {
x := [...]int{1, 2, 3, 4, 5}
fmt.Printf("x := %#v\n", x)
}
Notice that the output of the fmt.Printf() statement matches the *gofmt*'ed
version of the variable definition in the source code.
Here are a few scenarios where the two (*gofmt*'ed source and output) match
perfectly:
Source: x := 42
Output: x := 42
Source: x := []int(nil)
Output: x := []int(nil)
Source: x := []int{1, 2, 3, 4, 5}
Output: x := []int{1, 2, 3, 4, 5}
Source: x := [5]int{1, 2, 3, 4, 5}
Output: x := [5]int{1, 2, 3, 4, 5}
Source: x := [...]int{1, 2, 3, 4, 5}
Output: x := [5]int{1, 2, 3, 4, 5} // Not a perfect match, but it makes
sense, and this line is valid Go which can be compiled
Now this is where we start to see a divergence:
Source: x := map[string]int{"mon": 0, "tue": 1, "wed": 2}
Output: x := map[string]int{"mon":0, "tue":1, "wed":2}
Output: x := map[string]int{"mon":0, "tue":1, "wed":2}
Output: x := map[string]int{"mon":0, "wed":2, "tue":1}
Note the inconsistency in the spacing. Is this intentional, or shouldn't
the spacing format output of %#v match that of gofmt here?
Running the above input many times gives random output in terms of the
strings order. Sometimes it's mon-tue-wed, or it could be tue-mon-wed, or
wed-tue-mon, etc. I suppose this is probably fine and has an obvious
logical explanation (although it would be good to confirm).
The same spacing inconsistency applies to structs:
type Lang struct {
Name string
Year int
URL string
}
Source: x := Lang{Name: "Go", Year: 2009, URL: "http"}
Output: x := main.Lang{Name:"Go", Year:2009, URL:"http"} // Invalid Go
code, doesn't compile
Again, note the inconsistency in spacing.
This time, there's another difference. "main." is prepended to the type in
the output, and this isn't valid Go code, it gives an error:
main.Lang undefined (type func() has no field or method Lang)
Anyway, my *main* question is this:
*1. Why is there an inconsistency in spacing after colons in gofmt and
fmt's %#v and %+v printing modes?*
*Is it on purpose, then what's the reason?*
*If not on purpose, does it mean it's unintended/bug, so when will it be
fixed?*
*
*
I'd also like to know:
2. How come the map example output doesn't preserve order? (I know the
answer is likely trivial, as in maps don't have a strict ordering, but I'd
like to have this confirmed)
3. Why is the package name and period ("main." in above example) prepended
in %#v output, yet it is invalid Go code?
Thank you!
--