https://github.com/liamg/memoryfs
:brain: :file_cabinet: In-memory filesystem implementation of io/fs.FS
https://github.com/liamg/memoryfs
filesystem in-memory in-memory-filesystem
Last synced: 10 months ago
JSON representation
:brain: :file_cabinet: In-memory filesystem implementation of io/fs.FS
- Host: GitHub
- URL: https://github.com/liamg/memoryfs
- Owner: liamg
- License: mit
- Created: 2022-03-30T21:30:34.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-08-30T13:56:09.000Z (over 2 years ago)
- Last Synced: 2025-03-28T21:03:30.945Z (10 months ago)
- Topics: filesystem, in-memory, in-memory-filesystem
- Language: Go
- Homepage: https://pkg.go.dev/github.com/liamg/memoryfs
- Size: 32.2 KB
- Stars: 89
- Watchers: 4
- Forks: 17
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# memoryfs

An in-memory filesystem implementation of io/fs.FS.
`memoryfs` implements all of the currently defined `io/fs` interfaces:
- [fs.FS](https://pkg.go.dev/io/fs#FS)
- [fs.GlobFS](https://pkg.go.dev/io/fs#GlobFS)
- [fs.ReadDirFS](https://pkg.go.dev/io/fs#ReadDirFS)
- [fs.ReadFileFS](https://pkg.go.dev/io/fs#ReadFileFS)
- [fs.StatFS](https://pkg.go.dev/io/fs#StatFS)
- [fs.SubFS](https://pkg.go.dev/io/fs#SubFS)
It also allows the creation of files and directories.
## Example
```go
package main
import (
"fmt"
"io/fs"
"github.com/liamg/memoryfs"
)
func main() {
memfs := memoryfs.New()
if err := memfs.MkdirAll("my/dir", 0o700); err != nil {
panic(err)
}
if err := memfs.WriteFile("my/dir/file.txt", []byte("hello world"), 0o600); err != nil {
panic(err)
}
data, err := fs.ReadFile(memfs, "my/dir/file.txt")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
```
## Lazy Loading
If you are mirroring a disk file-system in memory, it can become very inefficient when large files are in use. For this scenario, the [WriteLazyFile](https://pkg.go.dev/github.com/liamg/memoryfs#FS.WriteLazyFile) method is recommended. It allows you to add a file whose content will be provided on-demand by calling the [LazyOpener](https://pkg.go.dev/github.com/liamg/memoryfs#LazyOpener) function.