| 1 |
--- |
| 2 |
title: strings.Diff |
| 3 |
description: Returns an anchored diff of the two texts OLD and NEW in the unified diff format. If OLD and NEW are identical, returns an empty string. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [strings.Diff OLDNAME OLD NEWNAME NEW] |
| 10 |
--- |
| 11 |
|
| 12 |
Use `strings.Diff` to compare two strings and render a highlighted diff: |
| 13 |
|
| 14 |
```go-html-template |
| 15 |
{{ $want := ` |
| 16 |
<p>The product of 6 and 7 is 42.</p> |
| 17 |
<p>The product of 7 and 6 is 42.</p> |
| 18 |
`}} |
| 19 |
|
| 20 |
{{ $got := ` |
| 21 |
<p>The product of 6 and 7 is 42.</p> |
| 22 |
<p>The product of 7 and 6 is 13.</p> |
| 23 |
`}} |
| 24 |
|
| 25 |
{{ $diff := strings.Diff "want" $want "got" $got }} |
| 26 |
{{ transform.Highlight $diff "diff" }} |
| 27 |
``` |
| 28 |
|
| 29 |
Rendered: |
| 30 |
|
| 31 |
 |