Diff Complex Data Structures? (structs, maps, slices, etc.)

8,588 views
Skip to first unread message

Felix Geisendoerfer

unread,
Jun 25, 2014, 6:40:55 AM6/25/14
to golan...@googlegroups.com
I'm looking for a pkg that would allow me to compare nested data structures (e.g. a struct that has a field which is a slice of pointers to another struct, etc.) and produce a human readable output highlighting the difference between the two inputs. [1]

This would be super handy for some of the unit testing I'm trying to do. So far I'm using reflect.DeepEqual() which does the job, but it's really painful to debug if the comparison fails.

Many thanks in advance!

Cheers,
Felix

Patrick Mézard

unread,
Jun 25, 2014, 7:59:40 AM6/25/14
to Felix Geisendoerfer, golang-nuts
We use a combination of go-spew ( https://github.com/davecgh/go-spew ), a regexp to drop pointers from go-spew output and go-difflib ( https://github.com/pmezard/go-difflib ) .


--
Patrick​

Felix Geisendoerfer

unread,
Jun 25, 2014, 9:37:18 AM6/25/14
to golan...@googlegroups.com, haim...@gmail.com
On Wednesday, June 25, 2014 1:59:40 PM UTC+2, patrick wrote:
We use a combination of go-spew ( https://github.com/davecgh/go-spew ), a regexp to drop pointers from go-spew output and go-difflib ( https://github.com/pmezard/go-difflib ) .

Good idea. Ideally this could be done without the regexp part. I've created an issue on the spew pkg [1]. Hopefully the author will agree to a Dump() variant that doesn't print pointer addresses. I'll try to release a pkg that integrates spew+difflib once I get this sorted.

Cheers,
Felix

Keith Rarick

unread,
Jun 25, 2014, 5:47:32 PM6/25/14
to Felix Geisendoerfer, golang-nuts
> I'm looking for a pkg that would allow me to compare nested data structures

I attempted something like this with
http://godoc.org/github.com/kr/pretty#Diff
http://godoc.org/github.com/kr/pretty#Fdiff

It is good enough to be sometimes useful in tests and debugging,
but definitely far from complete.

roger peppe

unread,
Jun 26, 2014, 4:46:54 AM6/26/14
to Felix Geisendoerfer, golang-nuts
The textual diff approach is interesting - I'd thought of doing
something more data-aware, but that's nice and generic.
It won't necessarily tell you what field the difference is found in though.

We currently use a different approach, which doesn't
give such complete results, but is useful in many cases.

See http://godoc.org/github.com/juju/testing/checkers#DeepEqual

It returns an error that describes where the deep-equal comparison fails
(it also compares a nil slice equal to an empty slice, which
is almost always what we want, because for testing purposes
we almost never want to distinguish the two)

The place it falls down is when you've got two long
slices that differ only in length. Perhaps a hybrid
approach could work well here - when there's a length
mismatch, resort to textual diff.

cheers,
rog.

Felix Geisendörfer

unread,
Jun 28, 2014, 5:51:13 AM6/28/14
to roger peppe, golang-nuts
Thanks for the additional suggestions, I'll experiment with these next week and report back.

Alfonso Vega-Garcia

unread,
Jun 28, 2014, 9:10:50 AM6/28/14
to Felix Geisendörfer, roger peppe, golang-nuts
https://github.com/kylelemons/godebug
works pretty well.


-- Alfonso

Alfonso Vega-Garcia | Software Engineer | vegacom at gmail.com
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages