# os.ReadDir
> 返回按文件名排序的 FileInfo 结构数组，每个目录条目对应一个元素。
- 官方英文原文：https://gohugo.io/functions/os/readdir/
- 本页规范地址：https://hugozh.cn/functions/os/readdir/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：os.ReadDir PATH
- 返回类型：os.FileInfo
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
`os.ReadDir` 函数相对于项目目录的根解析路径。路径开头的分隔符（`/`）是可选的。

目录结构如下：

```tree
content/
├── about.md
├── contact.md
└── news/
    ├── article-1.md
    └── article-2.md
```

以下模板代码：

```go-html-template
{{ range readDir "content" }}
  {{ .Name }} → {{ .IsDir }}
{{ end }}
```

输出：

```html
about.md → false
contact.md → false
news → true
```

注意，`os.ReadDir` 不会递归。

`FileInfo` 结构的详细信息见 [Go 文档][Go documentation]。

[Go documentation]: https://pkg.go.dev/io/fs#FileInfo

