strings.Diff
Returns an anchored diff of the two given texts in the unified diff format.
- Syntax
- strings.Diff OLDNAME OLD NEWNAME NEW
- Returns
- string
Usage
The strings.Diff function returns an anchored diff of the two texts OLD and NEW in the unified diff format, using OLDNAME and NEWNAME as the labels in the diff header. If OLD and NEW are identical, the function returns an empty string.
Examples
This example compares two HTML snippets, then passes the diff to the transform.Highlight function to render it with syntax highlighting:
{{ $want := `
<p>The product of 6 and 7 is 42.</p>
<p>The product of 7 and 6 is 42.</p>
` }}
{{ $got := `
<p>The product of 6 and 7 is 42.</p>
<p>The product of 7 and 6 is 13.</p>
` }}
{{ $diff := strings.Diff "want" $want "got" $got }}
{{ transform.Highlight $diff "diff" }}The rendered output:

Last updated:
November 25, 2026
:
content: Add lead-ins to first examples as needed per style guide (5e505b065)
Improve this page