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()
}
Most Helpful This Week
How to remove all line breaks from a string in Golang?
What is an HTTP client in Go?
How to remove special characters from a string in GoLang?
Launches 10 Goroutines and each goroutine adding 10 values to a Channel
GO Hello World program
Example of Interface with Type Embedding and Method Overriding in GO language
How to access slice items in Golang?
Cannot use <variable> as <type> value in return statement error in Golang
How do you handle HTTP client server alerting in Go?
Contains, ContainsAny, Count and EqualFold string functions in Go Language