sergi / go-diff

Diff, match and patch text in Go
MIT License
1.81k stars 207 forks source link

Get diff by words #121

Closed Taark closed 3 years ago

Taark commented 3 years ago

How to get get diff by words? Example: text1 := "some text" text2 := "other text" ... output []Diff: {Delete: some} {Insert: other} {Equal: text}

P.S. Sorry for my English.

Taark commented 3 years ago

Answer:

package main

import (
    "fmt"

    "github.com/sergi/go-diff/diffmatchpatch"
)

func main() {
    dmp := diffmatchpatch.New()
    text1 := "some text"
    text2 := "other text"

    diffs := dmp.DiffMain(text1, text2, true)
    fmt.Println(dmp.DiffCleanupSemantic(diffs))
}

I apologize for the stupid question.