File: ReadDir.md

package info (click to toggle)
hugo 0.131.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 45,580 kB
  • sloc: javascript: 31,172; xml: 248; makefile: 73; sh: 42
file content (49 lines) | stat: -rw-r--r-- 1,053 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
title: os.ReadDir
description: Returns an array of FileInfo structures sorted by file name, one element for each directory entry.
categories: []
keywords: []
action:
  aliases: [readDir]
  related:
    - functions/os/FileExists
    - functions/os/Getenv
    - functions/os/ReadFile
    - functions/os/Stat
  returnType: os.FileInfo
  signatures: [os.ReadDir PATH]
aliases: [/functions/readdir]
---

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:

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

This template code:

```go-html-template
{{ range 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).