Golang afero: Difference between revisions
From wikinotes
(→Basics) |
(→Basics) |
||
Line 22: | Line 22: | ||
// there are several additional options, like sftp, CopyOnWriteFs, ... | // there are several additional options, like sftp, CopyOnWriteFs, ... | ||
appfs.Create("/var/tmp/foo.txt') // | // 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") | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 17:55, 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
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()
).