mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
Merge commit '8d9511a08f14260cbfb73119e4afae50e5a9966d'
This commit is contained in:
@@ -1,28 +1,51 @@
|
||||
---
|
||||
title: readDir
|
||||
description: Gets a directory listing from a directory relative to the current working directory.
|
||||
date: 2017-02-01
|
||||
description: Returns an array of FileInfo structures sorted by filename, one element for each directory entry.
|
||||
publishdate: 2017-02-01
|
||||
lastmod: 2017-02-01
|
||||
lastmod: 2021-11-26
|
||||
categories: [functions]
|
||||
menu:
|
||||
docs:
|
||||
parent: "functions"
|
||||
keywords: [files]
|
||||
signature: ["readDir PATH"]
|
||||
signature: ["os.ReadDir PATH", "readDir PATH"]
|
||||
workson: []
|
||||
hugoversion:
|
||||
relatedfuncs: [readFile]
|
||||
relatedfuncs: ['os.FileExists','os.ReadFile','os.Stat']
|
||||
deprecated: false
|
||||
aliases: []
|
||||
---
|
||||
The `os.ReadDir` function resolves the path relative to the root of your project directory. A leading path separator (`/`) is optional.
|
||||
|
||||
If your current project working directory has a single file named `README.txt`:
|
||||
With this directory structure:
|
||||
|
||||
```
|
||||
{{ range (readDir ".") }}{{ .Name }}{{ end }} → "README.txt"
|
||||
```text
|
||||
content/
|
||||
├── about.md
|
||||
├── contact.md
|
||||
└── news/
|
||||
├── article-1.md
|
||||
└── article-2.md
|
||||
```
|
||||
|
||||
For more information on using `readDir` and `readFile` in your templates, see [Local File Templates][local].
|
||||
This template code:
|
||||
|
||||
[local]: /templates/files/
|
||||
```go-html-template
|
||||
{{ range os.ReadDir "content" }}
|
||||
{{ .Name }} --> {{ .IsDir }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
Produces:
|
||||
|
||||
```html
|
||||
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](https://pkg.go.dev/io/fs#FileInfo).
|
||||
|
||||
For more information on using `readDir` and `readFile` in your templates, see [Local File Templates]({{< relref "/templates/files" >}}).
|
||||
|
Reference in New Issue
Block a user