fmt.Printf Go-syntax formated representation of the value

325 views
Skip to first unread message

Jérôme LAFORGE

unread,
Apr 28, 2023, 2:02:42 AM4/28/23
to golang-nuts
Hello,
In my unit tests when expected is not the actual result, I like display actual value with Go-syntax. For example:
if got != expected {
     t.Errorf(" %#v", got) // []string{"blah','blah"}
}

But it want to know if there is elegant way to display as formatted on multi-lines (a kind of indented json but with gofmt rules)?

Thanks in advance,

Dan Kortschak

unread,
Apr 28, 2023, 4:46:13 AM4/28/23
to golan...@googlegroups.com
There are a bunch of pretty printers. I use github.com/kortschak/utter
which renders as Go syntax where possible. Another that attempts to do
the same thing with slightly different priorities is
github.com/jba/printsrc.

Wojciech S. Czarnecki

unread,
May 7, 2023, 9:42:54 AM5/7/23
to golan...@googlegroups.com, Dan Kortschak
Dnia 2023-04-28, o godz. 08:45:12
"'Dan Kortschak' via golang-nuts" <golan...@googlegroups.com> napisał(a):
> > But it want to know if there is elegant way to display as formatted
> > on multi-lines (a kind of indented json but with gofmt rules)?

Take a look here https://github.com/ohir/here and spice to taste.

Hope this helps,
[cc due to OP message age]

--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE

Merlins Owl

unread,
May 8, 2023, 12:47:58 PM5/8/23
to golang-nuts
Hello,

I've written a way for you to do it without using anything third-party. You can access and review from the link. Hope this helps;

Link → https://go.dev/play/p/EAURWpjaaw_o

Best,
7 Mayıs 2023 Pazar tarihinde saat 16:42:54 UTC+3 itibarıyla Wojciech S. Czarnecki şunları yazdı:
Reply all
Reply to author
Forward
0 new messages