Golang afero: Difference between revisions
From wikinotes
(→Basics) |
|||
Line 5: | Line 5: | ||
<blockquote> | <blockquote> | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |||
| api docs || https://pkg.go.dev/github.com/spf13/afero | |||
|- | |- | ||
| github || https://github.com/spf13/afero | | github || https://github.com/spf13/afero |
Revision as of 17:57, 22 July 2022
Afero defines and implements an interface to access the filesystem.
You can then pass in an abstraction of real os calls, or a stub stub interface you can make assertions against.
Documentation
api docs https://pkg.go.dev/github.com/spf13/afero github https://github.com/spf13/afero
Usage
Basics
import "github.com/spf13/afero" appfs := afero.NewOsFs() // real 'os' calls appfs := afero.NewMemMapFs() // memory backed fake filesystem // there are several additional options, like sftp, CopyOnWriteFs, ... // creates on disk, or in memory (depending on appfs val) appfs.Create("/var/tmp/foo.txt")// utils exposed in different imports import "afero/ioutil" ioutil.ReadFile(appfs, "/var/tmp/foo.txt")There are no assertions, when testing use the API to check the presence/contents of the file.
There are however some additional utils that are helpful, both in testing and production code (ex.appfs.DirExists()
).