Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

xml

go

perl

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?

like image 400
jnbek Avatar asked Sep 22 '12 01:09

jnbek


1 Answers

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/
like image 56
Jeremy Wall Avatar answered Nov 15 '22 22:11

Jeremy Wall