Standard library / io/fs

fs.ReadDirFS

Optional FS extension to list sorted directory entries directly. Expected stdout: 'a.txt <nil>\n'.

Formal Syntax & Structure

go syntaxgo
type ReadDirFS interface {  FS
  ReadDir(name string) ([]DirEntry, error)}

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() { var f fs.ReadDirFS = tree(); d, e := f.ReadDir("."); fmt.Println(d[0].Name(), e) }
Go Reference