有没有Go语言中类似于Perl的Data::Dumper模块中的Dumper()方法的等价物?

huangapple go评论91阅读模式
英文:

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

问题

我看过一个非常相似的帖子(Is there a C equivalent to Perls' Dumper() method in Data::Dumper?),关于C语言中与Data::Dumper::Dumper();等效的方法。我对Go语言也有类似的问题。

我是一个Perl狂热者,也是一个编程爱好者,每天都会使用Data::Dumper和类似的工具数百次。我开始学习Go语言,因为它看起来很有趣,很有意思,可以让我摆脱Perl的困境,同时让我对处理数据的新方法有更深入的了解...我真的很想要这样一个东西:

fmt.Println(dump.Dumper(decoded_json))

可以看到生成的数据结构,就像Data::Dumper会将JSON转换为数组和哈希表。在Go语言中看到这个,将帮助我理解如何构建和处理数据。这样的东西对我来说将是学习Go语言的重要时刻。

C中的帖子中所说的相反,我相信我们可以编写这个,并且在编译后,无论我传入和解码的是什么JSON字符串或XML页面,我都应该能够看到解码的结果,以类似于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?

答案1

得分: 12

你好,欢迎来到Go语言。我自己以前是Perl黑客。

关于你的问题,encoding/json包可能是最接近Go数据漂亮打印的工具了。不过我不确定你是否真的需要它。在Perl中,Data::Dumper非常棒的原因之一是因为很多时候你在消费数据时并不知道数据的结构,除非你直观地检查它。但是在Go中,每个东西都是一个特定的类型,每个特定的类型都有一个特定的结构。如果你想知道数据的样子,那么你可能只需要查看它的定义。

你还应该看看其他一些工具,包括:

  • fmt.Println("%#v", data)会以Go语法形式打印数据。
  • fmt.Println("%T", data)会以Go语法形式打印数据的类型。
  • 更多的fmt格式字符串选项可以在这里找到:http://golang.org/pkg/fmt/
英文:

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/

答案2

得分: 8

我找到了几个帮助在Go中可视化数据的包。

我个人最喜欢的是 - https://github.com/davecgh/go-spew

还有一个是 - https://github.com/tonnerre/golang-pretty

英文:

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

答案3

得分: 6

我对Perl和Dumper不熟悉,但根据我对你的帖子和相关的C帖子的理解(以及函数的名称!),它输出数据结构的内容。

你可以使用fmt包的%v动词来实现这一点。我假设你的JSON数据已解码为结构体或映射。使用fmt.Printf("%v", json_obj)将输出值,而%+v将添加字段名称(对于结构体而言没有区别,如果是映射,%v将同时输出键和值),而%#v将输出类型信息。

英文:

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.

huangapple
  • 本文由 发表于 2012年9月22日 09:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/12540057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定