Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pretty printed and highlighted structs easily?

Tags:

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

enter image description here

If its a dumb idea for even asking for pretty printed output please explain in brief.

like image 650
Stephen Nguyen Avatar asked Jan 30 '14 09:01

Stephen Nguyen


2 Answers

http://golang.org/pkg/fmt/

%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)
like image 93
elithrar Avatar answered Nov 04 '22 08:11

elithrar


Try github.com/davecgh/go-spew. It's like "%#v", but has much more prettier and detailed output.

like image 30
Kavu Avatar answered Nov 04 '22 08:11

Kavu