Help us shape the future of Go! Take the 2026 Go Developer Survey

app

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

README

golang-diff

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.

Features

  • 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.

Visual Comparison

Standard Diff (Unified)
--- file1.txt
+++ file2.txt
@@ -1,2 +1,2 @@
 Hello world
-This is a test
+This is a test case
golang-diff (Side-by-Side)
Hello 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.

Installation

To install the CLI tool:

go install github.com/arran4/golang-diff/cmd/diff@latest

This will install the diff binary to your $GOPATH/bin directory.

Usage (CLI)

The tool supports three main commands: compare, diff, and patch.

Compare Two Files

Compare two individual files side-by-side:

diff compare <file1> <file2> [flags]
Recursive Directory Diff

Compare two directories recursively:

diff diff <dir1> <dir2> [flags]

This will walk through both directories and display diffs for all modified files.

Apply Patch

Apply a patch generated by golang-diff to a target directory:

diff patch <patchFile> <targetDir>
Flags
  • --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).
Examples

Compare two files with color output:

diff compare file1.txt file2.txt -t

Compare two files interactively:

diff compare file1.txt file2.txt -i

Recursive diff:

diff diff src/ v1/src/

Output Guide

The output displays the two files side-by-side. The center column contains a symbol indicating the type of difference between the lines.

Symbols
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
Colors

When terminal mode is enabled (-t):

  • Green: Added or identical content.
  • Red: Deleted or modified content.
  • Yellow: Whitespace or EOL differences.
Example Output
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.

Usage (Library)

You can also use golang-diff as a library in your Go projects.

Import
import "github.com/arran4/golang-diff/pkg/diff"
Example
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)
}

Development

To run the tests:

go test ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareFiles

func CompareFiles(file1 string, file2 string, term bool, interactive bool, maxLines int)

CompareFiles is a subcommand `diff compare` Compares two files side by side.

Flags:

file1: @1 File 1 path
file2: @2 File 2 path
term: --term -t Terminal mode (colors)
interactive: --interactive -i Interactive mode
maxLines: --max-lines -m (default: 1000) Max lines to search for alignment

func DiffFiles

func DiffFiles(path1, path2 string, term bool, interactive bool, maxLines int, selectFile string)

DiffFiles is a subcommand 'diff diff' Compares two paths (files or directories) recursively.

Flags:

path1: @1 Path 1
path2: @2 Path 2
term: --term -t Terminal mode (colors)
interactive: --interactive -i Interactive mode
maxLines: --max-lines -m (default: 1000) Max lines to search for alignment
selectFile: --select-file -s Glob pattern to filter files

func PatchFiles

func PatchFiles(patchFile string, targetDir string)

PatchFiles is a subcommand 'diff patch' Applies a patch file to a target directory.

Flags:

patchFile: @1 Patch file path
targetDir: @2 Target directory path

Types

This section is empty.

Directories

Path Synopsis
cmd
diff command
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL