List directory in Go

前端 未结 6 727
夕颜
夕颜 2020-12-07 07:37

I\'ve been trying to figure out how to simply list the files and folders in a single directory in Go.

I\'ve found filepath.Walk, but it goes into sub-directories aut

6条回答
  •  爱一瞬间的悲伤
    2020-12-07 07:54

    Go 1.16 (Q1 2021) will propose, with CL 243908 and CL 243914 , the ReadDir function, based on the FS interface:

    // 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 fsutil.ReadFileFS, to provide additional or optimized functionality.
    // See io/fsutil for details.
    type FS interface {
        // 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.
        Open(name string) (File, error)
    }
    

    That allows for "os: add ReadDir method for lightweight directory reading":
    See commit a4ede9f:

    // ReadDir reads the contents of the directory associated with the file f
    // and returns a slice of DirEntry values in directory order.
    // Subsequent calls on the same file will yield later DirEntry records in the directory.
    //
    // If n > 0, ReadDir returns at most n DirEntry records.
    // In this case, if ReadDir returns an empty slice, it will return an error explaining why.
    // At the end of a directory, the error is io.EOF.
    //
    // If n <= 0, ReadDir returns all the DirEntry records remaining in the directory.
    // When it succeeds, it returns a nil error (not io.EOF).
    func (f *File) ReadDir(n int) ([]DirEntry, error) 
    
    // A DirEntry is an entry read from a directory (using the ReadDir method).
    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, 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 subdirectory.
        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() os.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)
    }
    

    src/os/os_test.go#testReadDir() illustrates its usage:

        file, err := Open(dir)
        if err != nil {
            t.Fatalf("open %q failed: %v", dir, err)
        }
        defer file.Close()
        s, err2 := file.ReadDir(-1)
        if err2 != nil {
            t.Fatalf("ReadDir %q failed: %v", dir, err2)
        }
    

提交回复
热议问题