Standard library / io/fs

fs.SubFS

Optional FS extension for subtree selection. Does not itself guarantee confinement of host symlinks. Expected stdout: 'go <nil>\n'.

Formal Syntax & Structure

go syntaxgo
type SubFS interface {  FS
  Sub(dir string) (FS, 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.SubFS = tree()  sub, e := f.Sub("sub")  if e != nil {    panic(e)  }  b, e := fs.ReadFile(sub, "b.txt")  fmt.Println(string(b), e)}
Go Reference