Doesn't it work already? http://play.golang.org/p/FEO-mpmZ_T
–-–
Alexey "AlekSi" Palazhchenko
Am Sonntag, 9. Dezember 2012 16:53:24 UTC+1 schrieb Alexey Palazhchenko:Doesn't it work already? http://play.golang.org/p/FEO-mpmZ_TOk, what I realy meant are pointer values, your slightly modified goplay :
http://play.golang.org/p/jkUZFmHM8V
Not so useful.
What should fmt.Printf() do in the case of a self referential data structure?At first step panic (once it will hit a recursion limit on the stack), or possibly at a later step, detect the cycle (as a garbage collector would too)
-j
–-–
Alexey "AlekSi" Palazhchenko
On Monday, December 10, 2012 8:14:05 AM UTC-6, rog wrote:i'd like to see a deep pretty printer for Go data structures, descending
into pointers too. since it's for debugging purposes, it doesn't need
to be too efficient - to avoid infinite loops on cycles, it should keep
track of previously-seen pointers and print a reference to the previously
printed value if encounters one it's already seen.
i've done this before in another language and it wasn't hard - i've
been meaning to get around to it for Go, but haven't done it yet.
Go's interior pointers make things a little more awkward - it's
probably worth doing two passes through the data, once to work
out where things are located, and the other to print things out with
appropriate cross-references in place.
making the output format clear and concise is a good challenge.
I have been working on a package that handles this as I find it useful as well.
One issue I've ran into though is that you can't invoke custom type Stringers on
unexported struct fields since you can't get an interface to them.
Unfortunately, that really hampers some of the usefulness of a deep pretty
printer for internal data structures while developing and debugging a package,
which is likely the main use case.
Here is some sample code demonstrating what I mean:
http://play.golang.org/p/bFAGv4M3Bf
Notice how the unexported field shows the raw uint8 value while the exported
field shows the pretty custom formatted version. In the second printf, the
unexported field is passed directly and, naturally, is able to make use of the
Stringer interface.
Is there some trick I'm missing that would allow this capability?
you need to use unsafe.0. optionally verify there is a Error/String() method on non-pointer receiver,so that your debug package won't accidentally damage your user's data1. dig out a pointer to that field (you might to dig it out by looking inside theinterface{} and utilizing structfield.Offset2. use reflect.NewAt (can't handle unexported types), or better, just make aninterface{} value yourself by using unsafe.3. cast it to Stringer and done.somewhat involved procedure, but really there isn't a better alternative.btw, i'd prefer the debug package to print out raw values, at least there shouldbe a mode to do this (ignoring String() and Error() methods).
Using text/tabwriter would be useful for scanning the output, so that types and values are aligned, not staggered.
I've finished and published the new package which handles this. I've also written a blog entry about it here: https://blog.cyphertite.com/go-spew-a-journey-into-dumping-go-data-structures/
i definitely want to be able to choose custom formatting
options without disturbing the global Config variable, although
using the global for a default seems fine (it could work something
like the flag package in that respect).
it would be quite nice to be able to see when one slice aliases another,
although working out a good syntax to represent that is a challenge.
in general, i wonder if it would be nice to print any reference only
once - if there's a reference from one part of the data structure,
it could print the reference only.
i was surprised when %#v didn't print the types. i definitely want to have
the ability to see types.
finally, "spew" is a great name for this. instantly memorable!
i definitely want to be able to choose custom formatting
options without disturbing the global Config variable, although
using the global for a default seems fine (it could work something
like the flag package in that respect).
Thanks for pointing this out. I'll have this case fixed shortly and add tests for it.
i was surprised when %#v didn't print the types. i definitely want to have
the ability to see types.
--