hugo/docs/content/en/functions/readdir.md
2021-12-08 08:54:25 +01:00

1.2 KiB

title description publishdate lastmod categories menu keywords signature workson hugoversion relatedfuncs deprecated aliases
readDir Returns an array of FileInfo structures sorted by filename, one element for each directory entry. 2017-02-01 2021-11-26
functions
docs
parent
functions
files
os.ReadDir PATH
readDir PATH
os.FileExists
os.ReadFile
os.Stat
false

The os.ReadDir function resolves the path relative to the root of your project directory. A leading path separator (/) is optional.

With this directory structure:

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

This template code:

{{ range os.ReadDir "content" }}
  {{ .Name }} --> {{ .IsDir }}
{{ end }}

Produces:

about.md --> false
contact.md --> false
news --> true

Note that os.ReadDir is not recursive.

Details of the FileInfo structure are available in the Go documentation.

For more information on using readDir and readFile in your templates, see [Local File Templates]({{< relref "/templates/files" >}}).