fs定义了文件系统的基本接口。文件系统可以由主机操作系统提供,也可以由其他包提供
Variables
var (
ErrInvalid = errInvalid() // "invalid argument"
ErrPermission = errPermission() // "permission denied"
ErrExist = errExist() // "file already exists"
ErrNotExist = errNotExist() // "file does not exist"
ErrClosed = errClosed() // "file already closed"
)
包函数
func Glob(fsys FS, pattern string) (matches []string, err error)
- 返回所有匹配pattern的文件名称,如果没有匹配的文件,则返回nil
func ReadFile(fsys FS, name string) ([]byte, error)
- ReadFile从文件系统fs中读取命名文件并返回其内容。一个成功的调用返回一个nil错误,而不是io.EOF。(因为ReadFile读取整个文件,最终读取的预期EOF不会被视为要报告的错误。)
func ValidPath(name string) bool 报告给定的路径名在调用Open时是否有效
type DirEntry
type DirEntry interface {
// For example, Name would return "hello.go" not "/home/gopher/hello.go".
Name() string
IsDir() bool
Type() FileMode
Info() (FileInfo, error)
}
func ReadDir(fsys FS, name string) ([]DirEntry, error) 读取指定目录并返回按文件名排序的目录条目列表
type FS
FS接口是文件系统所需的最低实现
一个文件系统可以实现额外的接口, 例如ReadFileFS,提供额外的或优化的功能
type FS interface {
Open(name string) (File, error)
}
func Sub(fsys FS, dir string) (FS, error) Sub返回一个对应于以fsys的目录为根的子树的FS
type File
type File interface {
Stat() (FileInfo, error)
Read([]byte) (int, error)
Close() error
}
type 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() interface{} // underlying data source (can return nil)
}
func Stat(fsys FS, name string) (FileInfo, error)
type FileMode
type FileMode uint32
const (
// The single letters are the abbreviations
// used by the String method's formatting.
ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
ModeAppend // a: append-only
ModeExclusive // l: exclusive use
ModeTemporary // T: temporary file; Plan 9 only
ModeSymlink // L: symbolic link
ModeDevice // D: device file
ModeNamedPipe // p: named pipe (FIFO)
ModeSocket // S: Unix domain socket
ModeSetuid // u: setuid
ModeSetgid // g: setgid
ModeCharDevice // c: Unix character device, when ModeDevice is set
ModeSticky // t: sticky
ModeIrregular // ?: non-regular file; nothing else is known about this file
// Mask for the type bits. For regular files, none will be set.
ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
ModePerm FileMode = 0777 // Unix permission bits
)
func (m FileMode) IsDir() bool
func (m FileMode) IsRegular() bool
func (m FileMode) Perm() FileMode
func (m FileMode) String() string
func (m FileMode) Type() FileMode
type PathError
PathError records an error and the operation and file path that caused it.
type PathError struct {
Op string
Path string
Err error
}
func (e PathError) Error() string
func (e PathError) Timeout() bool
func (e *PathError) Unwrap() error
type ReadDirFS
type ReadDirFS interface {
FS
// ReadDir reads the named directory
// and returns a list of directory entries sorted by filename.
ReadDir(name string) ([]DirEntry, error)
}
type ReadDirFile
type ReadDirFile interface {
File
// 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.
//
// 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.
ReadDir(n int) ([]DirEntry, error)
}
type ReadFileFS
type ReadFileFS interface {
FS
//读取成功应该返回的err应该是nil 而不是 io.EOF.
//因为他就是读文件的所有
ReadFile(name string) ([]byte, error)
}
type StatFS
type StatFS interface {
FS
Stat(name string) (FileInfo, error)
}
type SubFS
type SubFS interface {
FS
Sub(dir string) (FS, error)
}