golang-diff is a Go library and CLI tool for comparing text files line by line and character by character. It provides colored terminal output and an interactive viewer.
- Line Alignment: Uses a hash-based lookahead algorithm to align lines efficiently.
- Character-Level Diff: Computes precise character-level differences within modified lines.
- Terminal Colors: Highlighting for additions (green) and deletions (red).
- Interactive Mode: View differences in
less(or your preferred pager) for easy navigation. - Configurable Lookahead: Adjust the search depth for line alignment.
--- file1.txt
+++ file2.txt
@@ -1,2 +1,2 @@
Hello world
-This is a test
+This is a test caseHello world == Hello world
This is a test 1d This is a test case
Why it's better:
- Side-by-Side View: Compare changes in context without mental context switching.
- Character-Level Precision: Highlights exactly what changed within the line (e.g., " case" added).
- Line Alignment: Hash-based lookahead ensures lines are aligned even after large insertions/deletions.
To install the CLI tool:
go install github.com/arran4/golang-diff/cmd/diff@latestThis will install the diff binary to your $GOPATH/bin directory.
The tool supports three main commands: compare, diff, and patch.
Compare two individual files side-by-side:
diff compare <file1> <file2> [flags]Compare two directories recursively:
diff diff <dir1> <dir2> [flags]This will walk through both directories and display diffs for all modified files.
Apply a patch generated by golang-diff to a target directory:
diff patch <patchFile> <targetDir>--term/-t: Enable terminal colors (default: false).--interactive/-i: Open the diff in an interactive pager (less) (default: false).--max-lines/-m: Set the maximum number of lines to search ahead for alignment (default: 1000).
Compare two files with color output:
diff compare file1.txt file2.txt -tCompare two files interactively:
diff compare file1.txt file2.txt -iRecursive diff:
diff diff src/ v1/src/The output displays the two files side-by-side. The center column contains a symbol indicating the type of difference between the lines.
| Symbol | Meaning | Color (Term Mode) |
|---|---|---|
== |
Lines are identical | Green |
1d |
One continuous difference block | Red |
2d |
Two difference blocks | Red |
d |
Multiple difference blocks | Red |
w |
Whitespace difference only | Yellow |
q |
Mixed character and whitespace difference | Red |
$ |
End of Line (EOL) difference (e.g., CRLF vs LF) | Yellow |
When terminal mode is enabled (-t):
- Green: Added or identical content.
- Red: Deleted or modified content.
- Yellow: Whitespace or EOL differences.
Hello world == Hello world
This is a test 1d This is a test case
In this example:
- The first line is identical (
==). - The second line has one difference (
1d): " case" is added on the right side.
You can also use golang-diff as a library in your Go projects.
import "github.com/arran4/golang-diff/pkg/diff"package main
import (
"fmt"
"github.com/arran4/golang-diff/pkg/diff"
)
func main() {
text1 := "Hello world\nThis is a test"
text2 := "Hello World\nThis is a test case"
// Compare strings with options
output := diff.Compare(text1, text2, diff.TermMode(true))
fmt.Println(output)
}To run the tests:
go test ./...