Libra is a Go library to compare the interface (struct, maps, etc) and spot the differences between two of them.
$ go get -u github.com/haritsfahreza/libra
//Prepare the objects that you want to compare
oldPerson := Person{
Name: "Sudirman",
Age: 22,
Weight: float64(80),
IsMarried: true,
}
newPerson := Person{
Name: "Sudirman",
Age: 23,
Weight: float64(85),
IsMarried: true,
}
diffs, err := libra.Compare(context.Background(), oldPerson, newPerson)
if err != nil {
panic(err)
}
//Use the diffs array with your own purpose e.g. printout
for i, diff := range diffs {
fmt.Printf("#%d : ChangeType=%s Field=%s ObjectType=%s Old='%v' New='%v'\n", i, diff.ChangeType, diff.Field, diff.ObjectType, diff.Old, diff.New)
}
Please see examples for the other usage references
Currently, we need to have String
function to get the value of the struct with private fields since reflect
library would not be able to compare them.
type HiddenPerson struct {
name string
age int
}
func (h HiddenPerson) String() string {
return fmt.Sprintf("%s, %d", h.name, h.age)
}
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
- Harits Fahreza Christyonotoputra - Initial work - haritsfahreza
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details
- With
libra
, you can speed up your Go software development, especially when you build the object auditing and data versioning system. - This project is inspired by
Javers
.