英文:
how to pretty printed and highlighted structs easily?
问题
我现在正在写很多小脚本来学习Go语言,不管好坏,我已经习惯了以漂亮的格式显示数组、映射和切片等输出结果,这些结果通常会被高亮显示并且有缩进。
也许我应该使用http://golang.org/pkg/go/printer/#example_Fprint,但我不太确定如何使用它,也不确定它是否能给我想要的结果...
例如:Ruby的pry
如果我要求输出结果以漂亮的格式显示是一个愚蠢的想法,请简要解释一下。
英文:
I'm writing a lot of little scripts right now to learn go and for better or worse have gotten used to seeing output such as arrays, maps, slices in a nice highlighted, indented, pretty format.
I should maybe use http://golang.org/pkg/go/printer/#example_Fprint
but I'm not exactly sure how to use it nor if it gives me the result I'm looking for...
ex: ruby's pry
If its a dumb idea for even asking for pretty printed output please explain in brief.
答案1
得分: 23
> %v 以默认格式打印值。当打印结构体时,加号标志(%+v)会添加字段名
>
> %#v 以Go语法表示的值
示例代码如下:
fmt.Printf("%+v", mystruct)
英文:
> %v the value in a default format. when printing structs, the plus
> flag (%+v) adds field names
>
> %#v a Go-syntax representation of the value
Like so:
fmt.Printf("%+v", mystruct)
答案2
得分: 14
尝试使用github.com/davecgh/go-spew。它类似于"%#v"
,但输出更漂亮和详细。
英文:
Try github.com/davecgh/go-spew. It's like "%#v"
, but has much more prettier and detailed output.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论