readdir.md (1207B)
1 --- 2 title: readDir 3 description: Returns an array of FileInfo structures sorted by filename, one element for each directory entry. 4 publishdate: 2017-02-01 5 lastmod: 2021-11-26 6 categories: [functions] 7 menu: 8 docs: 9 parent: "functions" 10 keywords: [files] 11 signature: ["os.ReadDir PATH", "readDir PATH"] 12 workson: [] 13 hugoversion: 14 relatedfuncs: ['os.FileExists','os.ReadFile','os.Stat'] 15 deprecated: false 16 aliases: [] 17 --- 18 The `os.ReadDir` function resolves the path relative to the root of your project directory. A leading path separator (`/`) is optional. 19 20 With this directory structure: 21 22 ```text 23 content/ 24 ├── about.md 25 ├── contact.md 26 └── news/ 27 ├── article-1.md 28 └── article-2.md 29 ``` 30 31 This template code: 32 33 ```go-html-template 34 {{ range os.ReadDir "content" }} 35 {{ .Name }} --> {{ .IsDir }} 36 {{ end }} 37 ``` 38 39 Produces: 40 41 ```html 42 about.md --> false 43 contact.md --> false 44 news --> true 45 ``` 46 47 Note that `os.ReadDir` is not recursive. 48 49 Details of the `FileInfo` structure are available in the [Go documentation](https://pkg.go.dev/io/fs#FileInfo). 50 51 For more information on using `readDir` and `readFile` in your templates, see [Local File Templates]({{< relref "/templates/files" >}}).