Created
January 27, 2023 09:40
-
-
Save arriqaaq/a5bf6ee527aac4c5266050e122c1568a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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