• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

example/H07-Aug-2017-6254

testdata/H07-Aug-2017-118

.coveralls.ymlH A D07-Aug-201746 21

.gitignoreH A D07-Aug-2017266 2519

.travis.ymlH A D07-Aug-2017552 2722

CHANGELOG.mdH A D07-Aug-2017306 1510

LICENSEH A D07-Aug-20171.1 KiB2317

README.mdH A D07-Aug-20172.3 KiB9170

bypass.goH A D07-Aug-20175.6 KiB15265

bypasssafe.goH A D07-Aug-20171.6 KiB388

messagediff.goH A D07-Aug-20175.9 KiB243195

messagediff_test.goH A D07-Aug-20173.3 KiB194179

README.md

1# messagediff [![Build Status](https://travis-ci.org/d4l3k/messagediff.svg?branch=master)](https://travis-ci.org/d4l3k/messagediff) [![Coverage Status](https://coveralls.io/repos/github/d4l3k/messagediff/badge.svg?branch=master)](https://coveralls.io/github/d4l3k/messagediff?branch=master) [![GoDoc](https://godoc.org/github.com/d4l3k/messagediff?status.svg)](https://godoc.org/github.com/d4l3k/messagediff)
2
3A library for doing diffs of arbitrary Golang structs.
4
5If the unsafe package is available messagediff will diff unexported fields in
6addition to exported fields. This is primarily used for testing purposes as it
7allows for providing informative error messages.
8
9Optionally, fields in structs can be tagged as `testdiff:"ignore"` to make
10messagediff skip it when doing the comparison.
11
12
13## Example Usage
14In a normal file:
15```go
16package main
17
18import "gopkg.in/d4l3k/messagediff.v1"
19
20type someStruct struct {
21    A, b int
22    C []int
23}
24
25func main() {
26    a := someStruct{1, 2, []int{1}}
27    b := someStruct{1, 3, []int{1, 2}}
28    diff, equal := messagediff.PrettyDiff(a, b)
29    /*
30        diff =
31        `added: .C[1] = 2
32        modified: .b = 3`
33
34        equal = false
35    */
36}
37
38```
39In a test:
40```go
41import "gopkg.in/d4l3k/messagediff.v1"
42
43...
44
45type someStruct struct {
46    A, b int
47    C []int
48}
49
50func TestSomething(t *testing.T) {
51    want := someStruct{1, 2, []int{1}}
52    got := someStruct{1, 3, []int{1, 2}}
53    if diff, equal := messagediff.PrettyDiff(want, got); !equal {
54        t.Errorf("Something() = %#v\n%s", got, diff)
55    }
56}
57```
58To ignore a field in a struct, just annotate it with testdiff:"ignore" like
59this:
60```go
61package main
62
63import "gopkg.in/d4l3k/messagediff.v1"
64
65type someStruct struct {
66    A int
67    B int `testdiff:"ignore"`
68}
69
70func main() {
71    a := someStruct{1, 2}
72    b := someStruct{1, 3}
73    diff, equal := messagediff.PrettyDiff(a, b)
74    /*
75        equal = true
76        diff = ""
77    */
78}
79```
80
81See the `DeepDiff` function for using the diff results programmatically.
82
83## License
84Copyright (c) 2015 [Tristan Rice](https://fn.lc) <rice@fn.lc>
85
86messagediff is licensed under the MIT license. See the LICENSE file for more information.
87
88bypass.go and bypasssafe.go are borrowed from
89[go-spew](https://github.com/davecgh/go-spew) and have a seperate copyright
90notice.
91