How to read current directory using Readdir?
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.
Example
package main
import (
"log"
"os"
"fmt"
)
func readCurrentDir() {
file, err := os.Open(".")
if err != nil {
log.Fatalf("failed opening directory: %s", err)
}
defer file.Close()
fileList,_ := file.Readdir(0)
fmt.Printf("\nName\t\tSize\tIsDirectory Last Modification\n")
for _, files := range fileList {
fmt.Printf("\n%-15s %-7v %-12v %v", files.Name(), files.Size(), files.IsDir(), files.ModTime())
}
}
func main() {
readCurrentDir()
}
![](https://www.golangprograms.com/media/wysiwyg/example21.jpg)
Most Helpful This Week
Program to print full pyramid using star
How do you write multi-line strings in Go?
Exploring Blockchain: Top 15 Real-World Use Cases in 2024
How to remove multiple spaces in a string in GoLang?
Golang program for implementation of Radix Sort
Golang HTTP GET request with parameters
GO language program with example of Array Reverse Sort Functions for integer and strings
Cannot convert <type1> to <type2> error in Golang
Golang program for implementation of Shell Sort
Syntax error: unexpected <token> error in Golang