package fs

Import Path
	io/fs (on go.dev)

Dependency Relation
	imports 7 packages, and imported by 17 packages

Involved Source Files format.go Package fs defines basic interfaces to a file system. A file system can be provided by the host operating system but also by other packages. See the [testing/fstest] package for support with testing implementations of file systems. glob.go readdir.go readfile.go stat.go sub.go walk.go
Code Examples package main import ( "fmt" "io/fs" "log" "os" ) func main() { root := "/usr/local/go/bin" fileSystem := os.DirFS(root) fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error { if err != nil { log.Fatal(err) } fmt.Println(path) return nil }) }
Package-Level Type Names (total 13)
/* sort by: | */
A DirEntry is an entry read from a directory (using the [ReadDir] function or a [ReadDirFile]'s ReadDir method). Info returns the FileInfo for the file or subdirectory described by the entry. The returned FileInfo may be from the time of the original directory read or from the time of the call to Info. If the file has been removed or renamed since the directory read, Info may return an error satisfying errors.Is(err, ErrNotExist). If the entry denotes a symbolic link, Info reports the information about the link itself, not the link's target. IsDir reports whether the entry describes a directory. Name returns the name of the file (or subdirectory) described by the entry. This name is only the final element of the path (the base name), not the entire path. For example, Name would return "hello.go" not "home/gopher/hello.go". Type returns the type bits for the entry. The type bits are a subset of the usual FileMode bits, those returned by the FileMode.Type method. func FileInfoToDirEntry(info FileInfo) DirEntry func ReadDir(fsys FS, name string) ([]DirEntry, error) func ReadDirFile.ReadDir(n int) ([]DirEntry, error) func ReadDirFS.ReadDir(name string) ([]DirEntry, error) func embed.FS.ReadDir(name string) ([]DirEntry, error) func os.ReadDir(name string) ([]os.DirEntry, error) func os.(*File).ReadDir(n int) ([]os.DirEntry, error) func testing/fstest.MapFS.ReadDir(name string) ([]DirEntry, error) func FormatDirEntry(dir DirEntry) string
A File provides access to a single file. The File interface is the minimum implementation required of the file. Directory files should also implement [ReadDirFile]. A file may implement [io.ReaderAt] or [io.Seeker] as optimizations. ( File) Close() error ( File) Read([]byte) (int, error) ( File) Stat() (FileInfo, error) ReadDirFile (interface) net/http.File (interface) *os.File File : io.Closer File : io.ReadCloser File : io.Reader func FS.Open(name string) (File, error) func GlobFS.Open(name string) (File, error) func ReadDirFS.Open(name string) (File, error) func ReadFileFS.Open(name string) (File, error) func StatFS.Open(name string) (File, error) func SubFS.Open(name string) (File, error) func archive/zip.(*Reader).Open(name string) (File, error) func embed.FS.Open(name string) (File, error) func testing/fstest.MapFS.Open(name string) (File, error)
A FileInfo describes a file and is returned by [Stat]. // abbreviation for Mode().IsDir() // modification time // file mode bits // base name of the file // length in bytes for regular files; system-dependent for others // underlying data source (can return nil) func Stat(fsys FS, name string) (FileInfo, error) func DirEntry.Info() (FileInfo, error) func File.Stat() (FileInfo, error) func ReadDirFile.Stat() (FileInfo, error) func StatFS.Stat(name string) (FileInfo, error) func io/ioutil.ReadDir(dirname string) ([]FileInfo, error) func archive/tar.(*Header).FileInfo() FileInfo func archive/zip.(*FileHeader).FileInfo() FileInfo func net/http.File.Readdir(count int) ([]FileInfo, error) func net/http.File.Stat() (FileInfo, error) func os.Lstat(name string) (os.FileInfo, error) func os.Stat(name string) (os.FileInfo, error) func os.DirEntry.Info() (FileInfo, error) func os.(*File).Readdir(n int) ([]os.FileInfo, error) func os.(*File).Stat() (os.FileInfo, error) func testing/fstest.MapFS.Stat(name string) (FileInfo, error) func FileInfoToDirEntry(info FileInfo) DirEntry func FormatFileInfo(info FileInfo) string func archive/tar.FileInfoHeader(fi FileInfo, link string) (*tar.Header, error) func archive/zip.FileInfoHeader(fi FileInfo) (*zip.FileHeader, error) func os.SameFile(fi1, fi2 os.FileInfo) bool
A FileMode represents a file's mode and permission bits. The bits have the same definition on all systems, so that information about files can be moved from one system to another portably. Not all bits apply to all systems. The only required bit is [ModeDir] for directories. IsDir reports whether m describes a directory. That is, it tests for the [ModeDir] bit being set in m. IsRegular reports whether m describes a regular file. That is, it tests that no mode type bits are set. Perm returns the Unix permission bits in m (m & [ModePerm]). ( FileMode) String() string Type returns type bits in m (m & [ModeType]). FileMode : expvar.Var FileMode : fmt.Stringer func DirEntry.Type() FileMode func FileInfo.Mode() FileMode func FileMode.Perm() FileMode func FileMode.Type() FileMode func archive/zip.(*FileHeader).Mode() (mode FileMode) func os.DirEntry.Type() FileMode func os.FileInfo.Mode() FileMode func io/ioutil.WriteFile(filename string, data []byte, perm FileMode) error func archive/zip.(*FileHeader).SetMode(mode FileMode) func os.Chmod(name string, mode os.FileMode) error func os.Mkdir(name string, perm os.FileMode) error func os.MkdirAll(path string, perm os.FileMode) error func os.OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) func os.WriteFile(name string, data []byte, perm os.FileMode) error func os.(*File).Chmod(mode os.FileMode) error const ModeAppend const ModeCharDevice const ModeDevice const ModeDir const ModeExclusive const ModeIrregular const ModeNamedPipe const ModePerm const ModeSetgid const ModeSetuid const ModeSocket const ModeSticky const ModeSymlink const ModeTemporary const ModeType const os.ModeAppend const os.ModeCharDevice const os.ModeDevice const os.ModeDir const os.ModeExclusive const os.ModeIrregular const os.ModeNamedPipe const os.ModePerm const os.ModeSetgid const os.ModeSetuid const os.ModeSocket const os.ModeSticky const os.ModeSymlink const os.ModeTemporary const os.ModeType
An FS provides access to a hierarchical file system. The FS interface is the minimum implementation required of the file system. A file system may implement additional interfaces, such as [ReadFileFS], to provide additional or optimized functionality. [testing/fstest.TestFS] may be used to test implementations of an FS for correctness. Open opens the named file. When Open returns an error, it should be of type *PathError with the Op field set to "open", the Path field set to name, and the Err field describing the problem. Open should reject attempts to open names that do not satisfy ValidPath(name), returning a *PathError with Err set to ErrInvalid or ErrNotExist. GlobFS (interface) ReadDirFS (interface) ReadFileFS (interface) StatFS (interface) SubFS (interface) *archive/zip.ReadCloser *archive/zip.Reader embed.FS testing/fstest.MapFS func Sub(fsys FS, dir string) (FS, error) func SubFS.Sub(dir string) (FS, error) func os.DirFS(dir string) FS func testing/fstest.MapFS.Sub(dir string) (FS, error) func Glob(fsys FS, pattern string) (matches []string, err error) func ReadDir(fsys FS, name string) ([]DirEntry, error) func ReadFile(fsys FS, name string) ([]byte, error) func Stat(fsys FS, name string) (FileInfo, error) func Sub(fsys FS, dir string) (FS, error) func WalkDir(fsys FS, root string, fn WalkDirFunc) error func archive/tar.(*Writer).AddFS(fsys FS) error func archive/zip.(*Writer).AddFS(fsys FS) error func html/template.ParseFS(fs FS, patterns ...string) (*template.Template, error) func html/template.(*Template).ParseFS(fs FS, patterns ...string) (*template.Template, error) func net/http.FileServerFS(root FS) http.Handler func net/http.FS(fsys FS) http.FileSystem func net/http.NewFileTransportFS(fsys FS) http.RoundTripper func net/http.ServeFileFS(w http.ResponseWriter, r *http.Request, fsys FS, name string) func testing/fstest.TestFS(fsys FS, expected ...string) error func text/template.ParseFS(fsys FS, patterns ...string) (*template.Template, error) func text/template.(*Template).ParseFS(fsys FS, patterns ...string) (*template.Template, error)
A GlobFS is a file system with a Glob method. Glob returns the names of all files matching pattern, providing an implementation of the top-level Glob function. Open opens the named file. When Open returns an error, it should be of type *PathError with the Op field set to "open", the Path field set to name, and the Err field describing the problem. Open should reject attempts to open names that do not satisfy ValidPath(name), returning a *PathError with Err set to ErrInvalid or ErrNotExist. testing/fstest.MapFS GlobFS : FS
PathError records an error and the operation and file path that caused it. Err error Op string Path string (*PathError) Error() string Timeout reports whether this error represents a timeout. (*PathError) Unwrap() error *PathError : error
A ReadDirFile is a directory file whose entries can be read with the ReadDir method. Every directory file should implement this interface. (It is permissible for any file to implement this interface, but if so ReadDir should return an error for non-directories.) ( ReadDirFile) Close() error ( ReadDirFile) Read([]byte) (int, error) ReadDir reads the contents of the directory and returns a slice of up to n DirEntry values in directory order. Subsequent calls on the same file will yield further DirEntry values. If n > 0, ReadDir returns at most n DirEntry structures. In this case, if ReadDir returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF. (ReadDir must return io.EOF itself, not an error wrapping io.EOF.) If n <= 0, ReadDir returns all the DirEntry values from the directory in a single slice. In this case, if ReadDir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, ReadDir returns the DirEntry list read until that point and a non-nil error. ( ReadDirFile) Stat() (FileInfo, error) *os.File ReadDirFile : File ReadDirFile : io.Closer ReadDirFile : io.ReadCloser ReadDirFile : io.Reader
ReadDirFS is the interface implemented by a file system that provides an optimized implementation of [ReadDir]. Open opens the named file. When Open returns an error, it should be of type *PathError with the Op field set to "open", the Path field set to name, and the Err field describing the problem. Open should reject attempts to open names that do not satisfy ValidPath(name), returning a *PathError with Err set to ErrInvalid or ErrNotExist. ReadDir reads the named directory and returns a list of directory entries sorted by filename. embed.FS testing/fstest.MapFS ReadDirFS : FS
ReadFileFS is the interface implemented by a file system that provides an optimized implementation of [ReadFile]. Open opens the named file. When Open returns an error, it should be of type *PathError with the Op field set to "open", the Path field set to name, and the Err field describing the problem. Open should reject attempts to open names that do not satisfy ValidPath(name), returning a *PathError with Err set to ErrInvalid or ErrNotExist. ReadFile reads the named file and returns its contents. A successful call returns a nil error, not io.EOF. (Because ReadFile reads the whole file, the expected EOF from the final Read is not treated as an error to be reported.) The caller is permitted to modify the returned byte slice. This method should return a copy of the underlying data. embed.FS testing/fstest.MapFS ReadFileFS : FS
A StatFS is a file system with a Stat method. Open opens the named file. When Open returns an error, it should be of type *PathError with the Op field set to "open", the Path field set to name, and the Err field describing the problem. Open should reject attempts to open names that do not satisfy ValidPath(name), returning a *PathError with Err set to ErrInvalid or ErrNotExist. Stat returns a FileInfo describing the file. If there is an error, it should be of type *PathError. testing/fstest.MapFS StatFS : FS
A SubFS is a file system with a Sub method. Open opens the named file. When Open returns an error, it should be of type *PathError with the Op field set to "open", the Path field set to name, and the Err field describing the problem. Open should reject attempts to open names that do not satisfy ValidPath(name), returning a *PathError with Err set to ErrInvalid or ErrNotExist. Sub returns an FS corresponding to the subtree rooted at dir. testing/fstest.MapFS SubFS : FS
WalkDirFunc is the type of the function called by [WalkDir] to visit each file or directory. The path argument contains the argument to [WalkDir] as a prefix. That is, if WalkDir is called with root argument "dir" and finds a file named "a" in that directory, the walk function will be called with argument "dir/a". The d argument is the [DirEntry] for the named path. The error result returned by the function controls how [WalkDir] continues. If the function returns the special value [SkipDir], WalkDir skips the current directory (path if d.IsDir() is true, otherwise path's parent directory). If the function returns the special value [SkipAll], WalkDir skips all remaining files and directories. Otherwise, if the function returns a non-nil error, WalkDir stops entirely and returns that error. The err argument reports an error related to path, signaling that [WalkDir] will not walk into that directory. The function can decide how to handle that error; as described earlier, returning the error will cause WalkDir to stop walking the entire tree. [WalkDir] calls the function with a non-nil err argument in two cases. First, if the initial [Stat] on the root directory fails, WalkDir calls the function with path set to root, d set to nil, and err set to the error from [fs.Stat]. Second, if a directory's ReadDir method (see [ReadDirFile]) fails, WalkDir calls the function with path set to the directory's path, d set to an [DirEntry] describing the directory, and err set to the error from ReadDir. In this second case, the function is called twice with the path of the directory: the first call is before the directory read is attempted and has err set to nil, giving the function a chance to return [SkipDir] or [SkipAll] and avoid the ReadDir entirely. The second call is after a failed ReadDir and reports the error from ReadDir. (If ReadDir succeeds, there is no second call.) The differences between WalkDirFunc compared to [path/filepath.WalkFunc] are: - The second argument has type [DirEntry] instead of [FileInfo]. - The function is called before reading a directory, to allow [SkipDir] or [SkipAll] to bypass the directory read entirely or skip all remaining files and directories respectively. - If a directory read fails, the function is called a second time for that directory to report the error. func WalkDir(fsys FS, root string, fn WalkDirFunc) error func path/filepath.WalkDir(root string, fn WalkDirFunc) error
Package-Level Functions (total 10)
FileInfoToDirEntry returns a [DirEntry] that returns information from info. If info is nil, FileInfoToDirEntry returns nil.
FormatDirEntry returns a formatted version of dir for human readability. Implementations of [DirEntry] can call this from a String method. The outputs for a directory named subdir and a file named hello.go are: d subdir/ - hello.go
FormatFileInfo returns a formatted version of info for human readability. Implementations of [FileInfo] can call this from a String method. The output for a file named "hello.go", 100 bytes, mode 0o644, created January 1, 1970 at noon is -rw-r--r-- 100 1970-01-01 12:00:00 hello.go
Glob returns the names of all files matching pattern or nil if there is no matching file. The syntax of patterns is the same as in [path.Match]. The pattern may describe hierarchical names such as usr/*/bin/ed. Glob ignores file system errors such as I/O errors reading directories. The only possible returned error is [path.ErrBadPattern], reporting that the pattern is malformed. If fs implements [GlobFS], Glob calls fs.Glob. Otherwise, Glob uses [ReadDir] to traverse the directory tree and look for matches for the pattern.
ReadDir reads the named directory and returns a list of directory entries sorted by filename. If fs implements [ReadDirFS], ReadDir calls fs.ReadDir. Otherwise ReadDir calls fs.Open and uses ReadDir and Close on the returned file.
ReadFile reads the named file from the file system fs and returns its contents. A successful call returns a nil error, not [io.EOF]. (Because ReadFile reads the whole file, the expected EOF from the final Read is not treated as an error to be reported.) If fs implements [ReadFileFS], ReadFile calls fs.ReadFile. Otherwise ReadFile calls fs.Open and uses Read and Close on the returned [File].
Stat returns a [FileInfo] describing the named file from the file system. If fs implements [StatFS], Stat calls fs.Stat. Otherwise, Stat opens the [File] to stat it.
Sub returns an [FS] corresponding to the subtree rooted at fsys's dir. If dir is ".", Sub returns fsys unchanged. Otherwise, if fs implements [SubFS], Sub returns fsys.Sub(dir). Otherwise, Sub returns a new [FS] implementation sub that, in effect, implements sub.Open(name) as fsys.Open(path.Join(dir, name)). The implementation also translates calls to ReadDir, ReadFile, and Glob appropriately. Note that Sub(os.DirFS("/"), "prefix") is equivalent to os.DirFS("/prefix") and that neither of them guarantees to avoid operating system accesses outside "/prefix", because the implementation of [os.DirFS] does not check for symbolic links inside "/prefix" that point to other directories. That is, [os.DirFS] is not a general substitute for a chroot-style security mechanism, and Sub does not change that fact.
ValidPath reports whether the given path name is valid for use in a call to Open. Path names passed to open are UTF-8-encoded, unrooted, slash-separated sequences of path elements, like “x/y/z”. Path names must not contain an element that is “.” or “..” or the empty string, except for the special case that the root directory is named “.”. Paths must not start or end with a slash: “/x” and “x/” are invalid. Note that paths are slash-separated on all systems, even Windows. Paths containing other characters such as backslash and colon are accepted as valid, but those characters must never be interpreted by an [FS] implementation as path element separators.
WalkDir walks the file tree rooted at root, calling fn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by fn: see the [fs.WalkDirFunc] documentation for details. The files are walked in lexical order, which makes the output deterministic but requires WalkDir to read an entire directory into memory before proceeding to walk that directory. WalkDir does not follow symbolic links found in directories, but if root itself is a symbolic link, its target will be walked.
Package-Level Variables (total 7)
Generic file system errors. Errors returned by file systems can be tested against these errors using [errors.Is].
Generic file system errors. Errors returned by file systems can be tested against these errors using [errors.Is].
Generic file system errors. Errors returned by file systems can be tested against these errors using [errors.Is].
Generic file system errors. Errors returned by file systems can be tested against these errors using [errors.Is].
Generic file system errors. Errors returned by file systems can be tested against these errors using [errors.Is].
SkipAll is used as a return value from [WalkDirFunc] to indicate that all remaining files and directories are to be skipped. It is not returned as an error by any function.
SkipDir is used as a return value from [WalkDirFunc] to indicate that the directory named in the call is to be skipped. It is not returned as an error by any function.
Package-Level Constants (total 15)
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The single letters are the abbreviations used by the String method's formatting.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
The defined file mode bits are the most significant bits of the [FileMode]. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
Mask for the type bits. For regular files, none will be set.