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

linediff

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package linediff splits texts into lines and finds a shortest line edit script between two of them. It imports only the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Split

func Split(s string) []string

Split is s as lines, without a final empty line for a trailing newline. An empty s has no lines.

Types

type Op

type Op struct {
	Op   byte
	Line string
}

Op is one line of an edit script: Op is ' ' for a kept line, '-' for a line removed from a, '+' for a line added from b.

func Diff

func Diff(a, b []string) []Op

Diff is a shortest edit script from a to b, by Myers' O(ND) algorithm in linear space. The lines both start and end with are kept, and the ones between are compared while their count times the lines that change stays at most 1<<30; past that they are Replace. In each run of changes the removed lines come before the added ones, as git prints them.

func Replace

func Replace(a, b []string) []Op

Replace is the script that removes every line of a and then adds every line of b. It is a correct diff of any two texts, for when Diff would cost too much.

Jump to

Keyboard shortcuts

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