# Go 模板函数与语句
> 用这些函数与语句编写条件判断、循环及其它模板逻辑。
- 本页规范地址：https://hugozh.cn/functions/go-template/
- 最近更新：2026-10-02
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译

## 本章页面
- [and](https://hugozh.cn/functions/go-template/and/)：返回第一个假值参数；若所有参数都为真值，则返回最后一个参数。（Markdown：https://hugozh.cn/functions/go-template/and/index.md）
- [block](https://hugozh.cn/functions/go-template/block/)：定义模板，并在当前位置执行它。（Markdown：https://hugozh.cn/functions/go-template/block/index.md）
- [break](https://hugozh.cn/functions/go-template/break/)：终止最内层的 range 迭代，并跳过其余所有迭代。（Markdown：https://hugozh.cn/functions/go-template/break/index.md）
- [continue](https://hugozh.cn/functions/go-template/continue/)：终止最内层的 range 迭代，并继续执行下一次迭代。（Markdown：https://hugozh.cn/functions/go-template/continue/index.md）
- [define](https://hugozh.cn/functions/go-template/define/)：定义一个模板。（Markdown：https://hugozh.cn/functions/go-template/define/index.md）
- [else](https://hugozh.cn/functions/go-template/else/)：为 if、with 与 range 语句开启一个备用分支。（Markdown：https://hugozh.cn/functions/go-template/else/index.md）
- [end](https://hugozh.cn/functions/go-template/end/)：结束 if、with、range、block 与 define 语句。（Markdown：https://hugozh.cn/functions/go-template/end/index.md）
- [if](https://hugozh.cn/functions/go-template/if/)：当表达式为真值时执行该代码块。（Markdown：https://hugozh.cn/functions/go-template/if/index.md）
- [len](https://hugozh.cn/functions/go-template/len/)：返回字符串、切片、映射或集合的长度。（Markdown：https://hugozh.cn/functions/go-template/len/index.md）
- [not](https://hugozh.cn/functions/go-template/not/)：对唯一参数取布尔反。（Markdown：https://hugozh.cn/functions/go-template/not/index.md）
- [or](https://hugozh.cn/functions/go-template/or/)：返回第一个真值参数；若所有参数都为假值，则返回最后一个参数。（Markdown：https://hugozh.cn/functions/go-template/or/index.md）
- [range](https://hugozh.cn/functions/go-template/range/)：遍历非空集合，把上下文（点）依次绑定到每个元素并执行代码块。（Markdown：https://hugozh.cn/functions/go-template/range/index.md）
- [return](https://hugozh.cn/functions/go-template/return/)：终止当前模板的执行；在局部模板中使用时，还可返回给定的值（如果有）。（Markdown：https://hugozh.cn/functions/go-template/return/index.md）
- [template](https://hugozh.cn/functions/go-template/template/)：执行给定的模板，可选择传入上下文。（Markdown：https://hugozh.cn/functions/go-template/template/index.md）
- [try](https://hugozh.cn/functions/go-template/try/)：对给定表达式求值，并返回一个 TryValue 对象。（Markdown：https://hugozh.cn/functions/go-template/try/index.md）
- [urlquery](https://hugozh.cn/functions/go-template/urlquery/)：返回其参数文本表示的转义结果，适合嵌入 URL 查询串。（Markdown：https://hugozh.cn/functions/go-template/urlquery/index.md）
- [with](https://hugozh.cn/functions/go-template/with/)：若表达式为真值，则把上下文（点）绑定到该表达式并执行代码块。（Markdown：https://hugozh.cn/functions/go-template/with/index.md）

---
## 本章导读

本目录收录的是 Go 标准库 `text/template` 自带的函数与语句，Hugo 直接沿用它们的语义。它们负责模板的**控制流**：条件判断（`if`、`else`）、循环（`range`、`break`、`continue`）、上下文绑定（`with`）、模板定义与调用（`define`、`block`、`template`），以及逻辑运算（`and`、`or`、`not`）与 `len`、`urlquery` 等常用函数。

Hugo 对标准库做了少量扩展，其中 `return`、`try` 属于非标准语句，用法见各自页面。

