Fragment Description:

Exploring package os func (f *File) Readdir(n int) (fi []FileInfo, err error) Type FileInfo interface {...
Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order.
Subsequent calls on the same file will yield further FileInfos.
If n > 0, Readdir returns at most n FileInfo 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 FileInfo 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 FileInfo read until that point and a non-nil error.


Go Playground

Last update, on 2015, Fri 9 Oct, 16:15:34

/* ... <== see fragment description ... */

package main

import (

func main() {
    // slash := string(os.PathSeparator) //or,
    slash := string(filepath.Separator) //to be impervious to OS...
    dirname := "." + slash
    fmt.Printf("dirname is: %s\n", dirname)
    d, err := os.Open(dirname)
    if err != nil {
    filesinfo, err := d.Readdir(-1)
    if err != nil {
    /* type FileInfo interface {           Name() string       // base name of the file
               Size() int64        // 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)
    for _, fi := range filesinfo {
        if fi.Mode().IsRegular() {
            fmt.Printf("-----\nFileName: %s\nSize: \t%d bytes\n",
                fi.Name(), fi.Size())

/* Expected Output:
dirname is: .\
FileName: readDir.go
Size:  1543 bytes
FileName: Readdir_and_FileInfo.exe
Size:  2251264 bytes