json dump to help debugging

134 views
Skip to first unread message

bsr

unread,
Oct 12, 2015, 12:21:46 PM10/12/15
to golang-nuts

Hi,

Is there any library or code I can use to dump a go type (struct, pointer,..) for debugging purpose
My simple approach is below, but that occasionally get error like

Err: json.MarshalIndent(): json: unsupported type:util.TypeFn


because there could be fields which are not supported by json. In my case, it is ok to just skip that value, and show rest of the content, or say size, type of fields present.


so my intention is easy debugging, just to see the content during debugging and not cared about performance.


thanks.

```
//Dump dumps the value as json encoded
func Dump(v interface{}) string {

j, err := json.MarshalIndent(v, "", "\t")
if err != nil {
return "Err: json.MarshalIndent(): " + err.Error()
}
return string(j)
}
```

Dave Cheney

unread,
Oct 12, 2015, 12:33:08 PM10/12/15
to golang-nuts
I think json is the wrong hammer for this task, its vocabulary is too limited.

Try fmt.Printf("%#v\n", ...)

There is also a pretty printer called go-spew. I haven't used it personally, but others recommend it highly.

Thanks

Dave

bsr

unread,
Oct 12, 2015, 12:51:53 PM10/12/15
to golang-nuts
Thanks Dave, that indeed addressed my issue. Cheers.
Reply all
Reply to author
Forward
0 new messages