2019-10-21 04:22:28 -04:00
|
|
|
---
|
2023-10-20 03:42:39 -04:00
|
|
|
title: os.ReadDir
|
|
|
|
linkTitle: readDir
|
2023-07-29 05:15:54 -04:00
|
|
|
description: Returns an array of FileInfo structures sorted by file name, one element for each directory entry.
|
2019-10-21 04:22:28 -04:00
|
|
|
categories: [functions]
|
2023-10-20 03:42:39 -04:00
|
|
|
keywords: []
|
2019-10-21 04:22:28 -04:00
|
|
|
menu:
|
|
|
|
docs:
|
2023-05-22 10:43:12 -04:00
|
|
|
parent: functions
|
2023-10-20 03:42:39 -04:00
|
|
|
function:
|
|
|
|
aliases: [readDir]
|
|
|
|
returnType: FileInfo
|
|
|
|
signatures: [os.ReadDir PATH]
|
|
|
|
relatedFunctions:
|
|
|
|
- os.FileExists
|
|
|
|
- os.Getenv
|
|
|
|
- os.ReadDir
|
|
|
|
- os.ReadFile
|
|
|
|
- os.Stat
|
|
|
|
aliases: [/functions/readdir]
|
2019-10-21 04:22:28 -04:00
|
|
|
---
|
2023-10-20 03:42:39 -04:00
|
|
|
|
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" }}
|
2023-08-07 04:35:12 -04:00
|
|
|
{{ .Name }} → {{ .IsDir }}
|
2021-12-08 02:42:31 -05:00
|
|
|
{{ end }}
|
|
|
|
```
|
|
|
|
|
|
|
|
Produces:
|
|
|
|
|
|
|
|
```html
|
2023-10-20 03:42:39 -04:00
|
|
|
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
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
For more information on using `readDir` and `readFile` in your templates, see [Local File Templates](/templates/files).
|