davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
6.1k stars 367 forks source link

Request to add features that can suppress unexported fields #121

Open flw-cn opened 4 years ago

flw-cn commented 4 years ago

Sometimes I want the data structure I get from Dump () as I actually see it. This can be very useful for debugging APIs. I don't want to see data that I can't access at all.

I'm not sure if anyone mentioned something similar before. I tried searching but didn't find it.

Anyway, I implemented this on my fork and it seems to work fine. So I sent you a PR, I hope you can consider whether to merge or re-implement it by other means.

Thank you in advance.

nf-brentsaner commented 8 months ago

I'd like to +1 this but for a different use case; child struct types of a parent struct sometimes associate with the parent via an unexported field. Sometimes these parents are huge.

For example,

type AVeryLargeThing struct {
    // ...
    RelatedThings []*SmallThing
    // ...
}

type SmallThing struct {
    Foo string
    Bar *int
    parentThing *AVeryLargeThing
}

If I want to dump a SmallThing, I end up getting parentThing in the process. In the case of e.g. things containing tls.Configs, etc. in unexported fields this can get quite large, and one is left either marshaling SmallThing to a more text-friendly representation (e.g. JSON) or explicitly print/dump/logging each exported field explicitly just to avoid the unexported field.