# strings.Diff
> 以统一差异格式返回两段文本 OLD 与 NEW 的带锚点差异；两者完全相同时返回空字符串。
- 官方英文原文：https://gohugo.io/functions/strings/diff/
- 本页规范地址：https://hugozh.cn/functions/strings/diff/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：strings.Diff OLDNAME OLD NEWNAME NEW
- 返回类型：string
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
用 `strings.Diff` 比较两段字符串并渲染出带高亮的差异：

```go-html-template
{{ $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" }}
```

渲染结果：

![界面截图](diff-screen-capture.png)

