Go: 标准库 io/fs

2022-02-252026-07-081 出链2 引用

定义文件系统接口,描述行为,不提供任何实现。

统一面对不同来源的"文件树":本地目录、embed.FS、zip 文件、tar 文件、内存文件系统...

FS 打开文件;File可以读取内容,获取文件信息;DirEntry 表示目录里面的某个项

go
// go doc io/fs.FS
type FS interface {
	// Open opens the named file.
	// [File.Close] must be called to release any associated resources.
	//
	// 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.
	Open(name string) (File, error)
}
 
// go doc io/fs.File
type File interface {
	Stat() (FileInfo, error)
	Read([]byte) (int, error)
	Close() error
}
 
// go doc io/fs.FileInfo
type FileInfo interface {
	Name() string       // base name of the file
	Size() int64        // length in bytes for regular files; system-dependent for others
	Mode() FileMode     // file mode bits
	ModTime() time.Time // modification time
	IsDir() bool        // abbreviation for Mode().IsDir()
	Sys() any           // underlying data source (can return nil)
}
 
type DirEntry interface {
	// 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".
	Name() string
 
	// IsDir reports whether the entry describes a directory.
	IsDir() bool
 
	// 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.
	Type() FileMode
 
	// 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.
	Info() (FileInfo, error)
}

Go: 标准库所说,先提供最小能力的接口fs.FS,拓展能力使用额外接口表达:

go
// 支持 Glob method 的文件系统
type GlobFS interface{}
 
// 支持 ReadDir 增强 的文件系统
type ReadDirFS interface{}
 
// 支持 ReadFile 增强 的文件系统
type ReadFileFS interface{}
 
// 支持 ReadLink 增强 的文件系统
type ReadLinkFS interface{}
 
// 支持 Stat 增强 的文件系统
type StatFS interface{}
 
// 支持 Sub 增强 的文件系统
type SubFS interface{}
 
// === 分别对应fs提供的几个方法
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 ReadLink(fsys FS, name string) (string, error)
func Stat(fsys FS, name string) (FileInfo, error)
func Sub(fsys FS, dir string) (FS, error)

walk dir

遍历一个FS文件系统,对每个DirEntry文件项执行回调函数 WalkDirFunc

go
func WalkDir(fsys FS, root string, fn WalkDirFunc) error
 
type WalkDirFunc func(path string, d DirEntry, err error) error

评论