2019-10-21 04:22:28 -04:00
|
|
|
---
|
|
|
|
title: readDir
|
2021-12-08 02:42:31 -05:00
|
|
|
description: Returns an array of FileInfo structures sorted by filename, one element for each directory entry.
|
2019-10-21 04:22:28 -04:00
|
|
|
publishdate: 2017-02-01
|
2021-12-08 02:42:31 -05:00
|
|
|
lastmod: 2021-11-26
|
2019-10-21 04:22:28 -04:00
|
|
|
categories: [functions]
|
|
|
|
menu:
|
|
|
|
docs:
|
|
|
|
parent: "functions"
|
|
|
|
keywords: [files]
|
2021-12-08 02:42:31 -05:00
|
|
|
signature: ["os.ReadDir PATH", "readDir PATH"]
|
2019-10-21 04:22:28 -04:00
|
|
|
workson: []
|
|
|
|
hugoversion:
|
2021-12-08 02:42:31 -05:00
|
|
|
relatedfuncs: ['os.FileExists','os.ReadFile','os.Stat']
|
2019-10-21 04:22:28 -04:00
|
|
|
deprecated: false
|
|
|
|
aliases: []
|
|
|
|
---
|
2021-12-08 02:42:31 -05:00
|
|
|
The `os.ReadDir` function resolves the path relative to the root of your project directory. A leading path separator (`/`) is optional.
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2021-12-08 02:42:31 -05:00
|
|
|
With this directory structure:
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2021-12-08 02:42:31 -05:00
|
|
|
```text
|
|
|
|
content/
|
|
|
|
├── about.md
|
|
|
|
├── contact.md
|
|
|
|
└── news/
|
|
|
|
├── article-1.md
|
|
|
|
└── article-2.md
|
2019-10-21 04:22:28 -04:00
|
|
|
```
|
2021-12-08 02:42:31 -05:00
|
|
|
|
|
|
|
This template code:
|
|
|
|
|
|
|
|
```go-html-template
|
|
|
|
{{ range os.ReadDir "content" }}
|
|
|
|
{{ .Name }} --> {{ .IsDir }}
|
|
|
|
{{ end }}
|
|
|
|
```
|
|
|
|
|
|
|
|
Produces:
|
|
|
|
|
|
|
|
```html
|
|
|
|
about.md --> false
|
|
|
|
contact.md --> false
|
|
|
|
news --> true
|
2019-10-21 04:22:28 -04:00
|
|
|
```
|
|
|
|
|
2021-12-08 02:42:31 -05:00
|
|
|
Note that `os.ReadDir` is not recursive.
|
|
|
|
|
|
|
|
Details of the `FileInfo` structure are available in the [Go documentation](https://pkg.go.dev/io/fs#FileInfo).
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2021-12-08 02:42:31 -05:00
|
|
|
For more information on using `readDir` and `readFile` in your templates, see [Local File Templates]({{< relref "/templates/files" >}}).
|