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

diff

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package diff computes line diffs and formats them as unified diffs.

Lines compares two lists of lines with patience diff: lines that occur exactly once on both sides anchor the comparison, and the regions between anchors are compared again. A region without such lines is compared with Myers' algorithm, which finds a shortest edit script, in linear space. Patience diff keeps unrelated changes apart on the unique lines of code, such as declarations, and bounds the work on large files: a region too large for Myers' algorithm is replaced as a whole.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitLines

func SplitLines(text string) []string

SplitLines splits text into lines that keep their "\n" terminator; the last line has none if the text does not end with a newline.

func Unified

func Unified(oldName, newName, old, new string, context int) string

Unified returns the unified diff that turns old into new, with up to context unchanged lines around each change, or "" if they are equal. The header names the sides oldName and newName, for example "a/main.go" and "b/main.go", or "/dev/null" for a side that does not exist. A last line without a newline is marked as in diff and git.

Types

type Edit

type Edit struct {
	Op   Op
	A, B int
}

Edit is one line of an edit script. A is the index of the line in the old side for Equal and Delete, B its index in the new side for Equal and Insert; the other index is the position the line is at.

func Lines

func Lines(a, b []string) []Edit

Lines returns an edit script that turns a into b.

type Op

type Op int8

Op is the kind of an edit.

const (
	// Equal keeps a line of both sides.
	Equal Op = iota
	// Delete removes a line of the old side.
	Delete
	// Insert adds a line of the new side.
	Insert
)

Jump to

Keyboard shortcuts

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