# collections.First
> 返回给定切片或字符串的前 N 个元素。
- 官方英文原文：https://gohugo.io/functions/collections/first/
- 本页规范地址：https://hugozh.cn/functions/collections/first/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：collections.First N SLICE|STRING
- 返回类型：any
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
```go-html-template
{{ slice "a" "b" "c" | first 1 }} → [a]
{{ slice "a" "b" "c" | first 2 }} → [a b]
```

由于字符串实际上就是只读的字节切片，该函数可用于返回字符串开头指定数量的字节：

```go-html-template
{{ "abc" | first 1 }} → a
{{ "abc" | first 2 }} → ab
```

注意一个_字符_可能由多个_字节_组成：

```go-html-template
{{ "Schön" | first 3 }} → Sch
{{ "Schön" | first 4 }} → Sch\xc3
{{ "Schön" | first 5 }} → Schö
```

要在页面集合上使用 `collections.First` 函数：

```go-html-template
{{ range first 5 .Pages }}
  {{ .Render "summary" }}
{{ end }}
```

把 `N` 设为 0 可返回空切片：

```go-html-template
{{ $emptyPageCollection := first 0 .Pages }}
```

`first` 与 [`where`][] 一起使用：

```go-html-template
{{ range where .Pages "Section" "articles" | first 5 }}
  {{ .Render "summary" }}
{{ end }}
```

[`where`]: /functions/collections/where/

