Standard library / os
os.DirFS()
Creates a filesystem view rooted at an OS directory; relative roots follow working-directory changes and links may escape the root. Example uses a disposable temporary workspace. Expected stdout: 'abc\n'.
Formal Syntax & Structure
go syntaxgo
func DirFS(dir string) fs.FSParameters & Attributes
| Name | Type | Description |
|---|---|---|
| dir | string | Directory pathname. |
Return Value
fs.FS
Creates a filesystem view rooted at an OS directory; relative roots follow working-directory changes and links may escape the root.
Working Example
Example Snippetgo
package main
import ( "fmt" "io/fs" "os")
func check(e error) { if e != nil { panic(e) }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()() seed() b, e := fs.ReadFile(os.DirFS("."), "a.txt") check(e) fmt.Println(string(b))}