Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created January 27, 2023 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arriqaaq/a5bf6ee527aac4c5266050e122c1568a to your computer and use it in GitHub Desktop.
Save arriqaaq/a5bf6ee527aac4c5266050e122c1568a to your computer and use it in GitHub Desktop.
type FileSystem interface {
Open() (io.ReadCloser, error)
List() ([]string, error)
}
type File struct {
name string
path string
}
func (f *File) Open() (io.ReadCloser, error) {
return os.Open(f.path)
}
func (f *File) List() ([]string, error) {
return nil, fmt.Errorf("%s is a file, not a directory", f.name)
}
type Directory struct {
name string
path string
files []FileSystem
}
func (d *Directory) Open() (io.ReadCloser, error) {
return nil, fmt.Errorf("%s is a directory, not a file", d.name)
}
func (d *Directory) List() ([]string, error) {
names := make([]string, len(d.files))
for i, file := range d.files {
names[i] = file.Name()
}
return names, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment