Skip to content

Instantly share code, notes, and snippets.

@elevine
Created July 26, 2016 20:07
Show Gist options
  • Select an option

  • Save elevine/dfc5ccc68afbafd0c4857feebec83100 to your computer and use it in GitHub Desktop.

Select an option

Save elevine/dfc5ccc68afbafd0c4857feebec83100 to your computer and use it in GitHub Desktop.
How to mock the filesystem in Go, from https://talks.golang.org/2012/10things.slide#8
var fs fileSystem = osFS{}
type fileSystem interface {
Open(name string) (file, error)
Stat(name string) (os.FileInfo, error)
}
type file interface {
io.Closer
io.Reader
io.ReaderAt
io.Seeker
Stat() (os.FileInfo, error)
}
// osFS implements fileSystem using the local disk.
type osFS struct{}
func (osFS) Open(name string) (file, error) { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment