HUGO
Menu
GitHub 90498 stars Mastodon

os.ReadDir

Returns a slice of FileInfo objects sorted by file name, one for each entry in the given directory.
Syntax
os.ReadDir PATH
Returns
[]os.FileInfo
Alias
readDir

Usage

The os.ReadDir function returns a slice of FileInfo objects sorted by file name, one for each entry in the given directory. The function resolves the PATH relative to the root of your project directory. A leading path separator (/) is optional. The function is not recursive, and it returns an empty slice if the directory does not exist.

Each FileInfo object has methods such as IsDir, ModTime, Name, and Size, as described in the Go documentation.

Examples

These examples use this content structure:

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

Listing the entries in the content directory:

{{ range os.ReadDir "content" }}
  {{ .Name }} → {{ .IsDir }}
{{ end }}

The rendered output:

about.md → false
contact.md → false
news → true