Standard library / io/fs

fs.ReadFileFS

Optional FS extension to read whole file directly; success returns nil error, not EOF. Expected stdout: 'ok <nil>\n'.

Formal Syntax & Structure

go syntaxgo
type ReadFileFS interface {  FS
  ReadFile(name string) ([]byte, 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.ReadFileFS = tree(); b, e := f.ReadFile("a.txt"); fmt.Println(string(b), e) }
Go Reference