readDir

将以下英文翻译为中文:

readDir

https://gohugo.io/functions/readdir/

Returns an array of FileInfo structures sorted by filename, one element for each directory entry.

语法

os.ReadDir PATH
readDir PATH

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:

1
2
3
4
5
6
content/
├── about.md
├── contact.md
└── news/
    ├── article-1.md
    └── article-2.md

This template code:

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

Produces:

1
2
3
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.

另请参阅