Golang afero: Difference between revisions

From wikinotes
(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...")
 
Line 16: Line 16:
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
import "github.com/spf13/afero"
appfs := afero.NewOsFs()    // real 'os' calls
appfs := afero.NewOsFs()    // real 'os' calls
appfs := afero.NewMemMapFs() // memory backed fake filesystem
appfs := afero.NewMemMapFs() // memory backed fake filesystem

Revision as of 16:49, 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, ...

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.