Golang afero

From wikinotes
Revision as of 16:49, 22 July 2022 by Will (talk | contribs) (Created page with "Afero defines and implements an interface to access the filesystem.<br> You can then pass in an abstraction of real os calls, or a stub stub interface you can make assertions against. = Documentation = <blockquote> {| class="wikitable" |- | github || https://github.com/spf13/afero |- |} </blockquote><!-- Documentation --> = Usage = <blockquote> == Basics == <blockquote> <syntaxhighlight lang="go"> appfs := afero.NewOsFs() // real 'os' calls appfs := afero.NewMemMap...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

appfs := afero.NewOsFs()     // real 'os' calls
appfs := afero.NewMemMapFs() // memory backed fake filesystem
// there are several additional options, like sftp, CopyOnWriteFs, ...

appfs.Create("/var/tmp/foo.txt') // either creates on disk, or in memory

There are no assertions, when testing use the API to check the presence/contents of the file.