Standard library / io/fs

fs.File

Open file contract combining Stat, Read and Close; directory reading is an optional additional interface. Expected stdout: 'ok\n'.

Formal Syntax & Structure

go syntaxgo
type File interface {  Stat() (FileInfo, error)  Read([]byte) (int, error)  Close() error}

Working Example

Example Snippetgo
package main
import (  "fmt"  "io"  "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.File  f, e := tree().Open("a.txt")  if e != nil {    panic(e)  }  defer f.Close()  b, _ := io.ReadAll(f)  fmt.Println(string(b))}
Go Reference