fs定义了文件系统的基本接口。文件系统可以由主机操作系统提供,也可以由其他包提供

Variables

  1. var (
  2. ErrInvalid = errInvalid() // "invalid argument"
  3. ErrPermission = errPermission() // "permission denied"
  4. ErrExist = errExist() // "file already exists"
  5. ErrNotExist = errNotExist() // "file does not exist"
  6. ErrClosed = errClosed() // "file already closed"
  7. )

包函数

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

  1. type DirEntry interface {
  2. // For example, Name would return "hello.go" not "/home/gopher/hello.go".
  3. Name() string
  4. IsDir() bool
  5. Type() FileMode
  6. Info() (FileInfo, error)
  7. }

func ReadDir(fsys FS, name string) ([]DirEntry, error) 读取指定目录并返回按文件名排序的目录条目列表

type FS

FS接口是文件系统所需的最低实现
一个文件系统可以实现额外的接口, 例如ReadFileFS,提供额外的或优化的功能

  1. type FS interface {
  2. Open(name string) (File, error)
  3. }

func Sub(fsys FS, dir string) (FS, error) Sub返回一个对应于以fsys的目录为根的子树的FS

type File

  1. type File interface {
  2. Stat() (FileInfo, error)
  3. Read([]byte) (int, error)
  4. Close() error
  5. }

type FileInfo

  1. type FileInfo interface {
  2. Name() string // base name of the file
  3. Size() int64 // length in bytes for regular files; system-dependent for others
  4. Mode() FileMode // file mode bits
  5. ModTime() time.Time // modification time
  6. IsDir() bool // abbreviation for Mode().IsDir()
  7. Sys() interface{} // underlying data source (can return nil)
  8. }

func Stat(fsys FS, name string) (FileInfo, error)

type FileMode

  1. type FileMode uint32
  1. const (
  2. // The single letters are the abbreviations
  3. // used by the String method's formatting.
  4. ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
  5. ModeAppend // a: append-only
  6. ModeExclusive // l: exclusive use
  7. ModeTemporary // T: temporary file; Plan 9 only
  8. ModeSymlink // L: symbolic link
  9. ModeDevice // D: device file
  10. ModeNamedPipe // p: named pipe (FIFO)
  11. ModeSocket // S: Unix domain socket
  12. ModeSetuid // u: setuid
  13. ModeSetgid // g: setgid
  14. ModeCharDevice // c: Unix character device, when ModeDevice is set
  15. ModeSticky // t: sticky
  16. ModeIrregular // ?: non-regular file; nothing else is known about this file
  17. // Mask for the type bits. For regular files, none will be set.
  18. ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
  19. ModePerm FileMode = 0777 // Unix permission bits
  20. )

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.

  1. type PathError struct {
  2. Op string
  3. Path string
  4. Err error
  5. }

func (e PathError) Error() string
func (e
PathError) Timeout() bool
func (e *PathError) Unwrap() error

type ReadDirFS

  1. type ReadDirFS interface {
  2. FS
  3. // ReadDir reads the named directory
  4. // and returns a list of directory entries sorted by filename.
  5. ReadDir(name string) ([]DirEntry, error)
  6. }

type ReadDirFile

  1. type ReadDirFile interface {
  2. File
  3. // ReadDir reads the contents of the directory and returns
  4. // a slice of up to n DirEntry values in directory order.
  5. // Subsequent calls on the same file will yield further DirEntry values.
  6. //
  7. // If n > 0, ReadDir returns at most n DirEntry structures.
  8. // In this case, if ReadDir returns an empty slice, it will return
  9. // a non-nil error explaining why.
  10. // At the end of a directory, the error is io.EOF.
  11. //
  12. // If n <= 0, ReadDir returns all the DirEntry values from the directory
  13. // in a single slice. In this case, if ReadDir succeeds (reads all the way
  14. // to the end of the directory), it returns the slice and a nil error.
  15. // If it encounters an error before the end of the directory,
  16. // ReadDir returns the DirEntry list read until that point and a non-nil error.
  17. ReadDir(n int) ([]DirEntry, error)
  18. }

type ReadFileFS

  1. type ReadFileFS interface {
  2. FS
  3. //读取成功应该返回的err应该是nil 而不是 io.EOF.
  4. //因为他就是读文件的所有
  5. ReadFile(name string) ([]byte, error)
  6. }

type StatFS

  1. type StatFS interface {
  2. FS
  3. Stat(name string) (FileInfo, error)
  4. }

type SubFS

  1. type SubFS interface {
  2. FS
  3. Sub(dir string) (FS, error)
  4. }