Standard library / io/fs
fs.ReadDir()
Returns directory entries sorted by filename. May dispatch to ReadDirFS or use the opened file's ReadDir method. Expected stdout: 'a.txt sub <nil>\n'.
Formal Syntax & Structure
go syntaxgo
func ReadDir(fsys FS, name string) ([]DirEntry, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fsys | FS | Filesystem implementing fs.FS. |
| name | string | Slash-separated filesystem path. |
Return Value
([]DirEntry, error)
Returns directory entries sorted by filename.
Working Example
Example Snippetgo
package main
import ( "fmt" "io/fs" "testing/fstest" "time")
func tree() fstest.MapFS { return fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("ok"), Mode: 0644, ModTime: time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)}, "sub/b.txt": &fstest.MapFile{Data: []byte("go"), Mode: 0644}}}func main() { d, e := fs.ReadDir(tree(), "."); fmt.Println(d[0].Name(), d[1].Name(), e) }