| 1 |
# memfs: A simple in-memory io/fs.FS filesystem |
| 2 |
|
| 3 |
memfs is an in-memory implementation of Go's io/fs.FS interface. |
| 4 |
The goal is to make it easy and quick to build an fs.FS filesystem |
| 5 |
when you don't have any complex requirements. |
| 6 |
|
| 7 |
Documentation: https://pkg.go.dev/github.com/psanford/memfs |
| 8 |
|
| 9 |
`io/fs` docs: https://tip.golang.org/pkg/io/fs/ |
| 10 |
|
| 11 |
## Usage |
| 12 |
|
| 13 |
``` |
| 14 |
package main |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"io/fs" |
| 19 |
|
| 20 |
"github.com/psanford/memfs" |
| 21 |
) |
| 22 |
|
| 23 |
func main() { |
| 24 |
rootFS := memfs.New() |
| 25 |
|
| 26 |
err := rootFS.MkdirAll("dir1/dir2", 0777) |
| 27 |
if err != nil { |
| 28 |
panic(err) |
| 29 |
} |
| 30 |
|
| 31 |
err = rootFS.WriteFile("dir1/dir2/f1.txt", []byte("incinerating-unsubstantial"), 0755) |
| 32 |
if err != nil { |
| 33 |
panic(err) |
| 34 |
} |
| 35 |
|
| 36 |
err = fs.WalkDir(rootFS, ".", func(path string, d fs.DirEntry, err error) error { |
| 37 |
fmt.Println(path) |
| 38 |
return nil |
| 39 |
}) |
| 40 |
if err != nil { |
| 41 |
panic(err) |
| 42 |
} |
| 43 |
|
| 44 |
content, err := fs.ReadFile(rootFS, "dir1/dir2/f1.txt") |
| 45 |
if err != nil { |
| 46 |
panic(err) |
| 47 |
} |
| 48 |
fmt.Printf("%s\n", content) |
| 49 |
} |
| 50 |
``` |