# len
> 返回字符串、切片、映射或集合的长度。
- 官方英文原文：https://gohugo.io/functions/go-template/len/
- 本页规范地址：https://hugozh.cn/functions/go-template/len/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：len VALUE
- 返回类型：int
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
## 用法

字符串：

```go-html-template
{{ "ab" | len }} → 2
{{ ""   | len }} → 0
```

切片：

```go-html-template
{{ slice "a" "b" | len }} → 2
{{ slice         | len }} → 0
```

映射：

```go-html-template
{{ dict "a" 1 "b" 2 | len }} → 2
{{ dict             | len }} → 0
```

集合：

```go-html-template
{{ site.RegularPages | len }} → 42
```

也可以用下面的写法统计集合中的页面数量：

```go-html-template
{{ site.RegularPages.Len }} → 42
```

更多信息参见 Go 的 [`text/template`](https://pkg.go.dev/text/template) 文档。

