package main
import (
"fmt"
"io/ioutil"
)
func FormatSize(size int64) string {
units := [6]string{"B", "KB", "MB", "GB", "TB", "PB"}
fsize := float64(size)
unit := float64(1024)
index := 0
// fisze > 1024KB, fsize <1024B
for fsize >= unit && index < len(units)-1 {
fsize /= unit
index++
}
return fmt.Sprintf("%.2f%s", fsize, units[index])
}
func main() {
files, err := ioutil.ReadDir(".")
fmt.Println(err)
for _, file := range files {
if file.IsDir() {
fmt.Println("D", file.Name(), file.ModTime().Format("2006-01-02 15:03:04"))
} else {
//fmt.Println(file.Size())
fmt.Println(FormatSize(file.Size()))
}
}
}