Is there a Go Language equivalent to Perls' Dumper() method in Data::Dumper?

I've looked at the very similarly titled post (Is there a C equivalent to Perls' Dumper() method in Data::Dumper?), regarding a C equivalent to Data::Dumper::Dumper();. I have a similar question for the Go language.

I'm a Perl Zealot by trade, and am a progamming hobbyist, and make use of Data::Dumper and similar offspring literally hundreds of times a day. I've taken up learning Go, because it looks like a fun and interesting language, something that will get me out of the Perl rut I'm in, while opening my eyes to new ways of doing stuffz... One of the things I really want is something like:

fmt.Println(dump.Dumper(decoded_json))

to see the resulting data structure, like Data::Dumper would turn the JSON into an Array of Hashes. Seeing this in Go, will help me to understand how to construct and work with the data. Something like this would be considered a major lightbulb moment in my learning of Go.

Contrary to the statements made in the C counterpart post, I believe we can write this, and since I'll be passing Dumper to Println, after compilation what ever JSON string or XML page I pass in and decode. I should be able to see the result of the decoding, in a Dumper like state... So, does any more know of anything like this that exists? or maybe some pointers to getting something like this done?

Answers


I found a couple packages to help visualize data in Go.

My personal favourite - https://github.com/davecgh/go-spew

There's also - https://github.com/tonnerre/golang-pretty


Hi and welcome to go I'm former perl hacker myself.

As to your question the encoding/json package is probably the closest you will find to a go data pretty printer. I'm not sure you really need it though. One of the reasons Data::Dumper was awesome in perl is because many times you really didn't know the structure of the data you were consuming without visually inspecting it. With go though everything is a specific type and every specific type has a specific structure. If you want to know what the data will look like then you probably just need to look at it's definition.

Some other tools you should look at include:

  • fmt.Println("%#v", data) will print the data in go-syntax form.
  • fmt.Println("%T", data) will print the data's type in go-syntax form.
  • More fmt format string options are documented here: http://golang.org/pkg/fmt/

I'm not familiar with Perl and Dumper, but from what I understand of your post and the related C post (and the very name of the function!), it outputs the content of the data structure.

You can do this using the %v verb of the fmt package. I assume your JSON data is decoded into a struct or a map. Using fmt.Printf("%v", json_obj) will output the values, while %+v will add field names (for a struct - no difference if its a map, %v will output both keys and values), and %#v will output type information too.


Need Your Help

Collecting all or at least a good sample of tweets related to a particular event

twitter tweets twitter-streaming-api twitter-search

I'm interested in using the Tweets in a Complex Networks research project for which I've to collect a good amount of tweets(unbiased sample of tweets) that're related to a particular event, for exa...