Simple generic diff algorithm for Go.
$ go get -v -u github.com/chakrit/gendiff
L
orLeft
- Values on the left side of things. The "base" values.R
orRight
- Values on the right side of things. The "new" or "changed" values.Match
- Item on the left matches the one on the right.Insert
- Item on the right was not present on the left, it has been "inserted".Delete
- Item on the left was not present on the right, it has been "deleted".
- Implement
gendiff.Interface
on the values you wish to generate diffs from. - Call
gendiff.Make()
to generate the diffs. - Loop on the resulting
[]gendiff.Diff
to inspect the diff. Switch on theOp
field to determine what the diff entry
switch d.Op {
case gendiff.Match:
for i := d.Lstart; i < d.Lend; i++ {
fmt.Println(" "+compare.LeftLines[i])
}
case gendiff.Delete:
for i := d.Lstart; i < d.Lend; i++ {
fmt.Println("--- "+compare.LeftLines[i])
}
case gendiff.Insert:
for i := d.Rstart; i < d.Rend; i++ {
fmt.Println("+++ "+compare.RightLines[i])
}
}
See examples/main.go
for a bit more complete example.
MIT