This is an education project that attempts to reimplement the GNU coreutils in Go. You can find the full manual here: https://www.gnu.org/software/coreutils/manual/coreutils.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
go-coreutils/du/main.go

45 lines
832 B

package main
import (
"fmt"
"log"
"flag"
"io/fs"
"path/filepath"
)
func StatDir(target_path string) error {
var cur_dir string
var cur_size int64
// NOTE: cover the difference betweeen filepath.WalkDir and fs.WalkDir
err := filepath.WalkDir(target_path, func (path string, d fs.DirEntry, err error) error {
if d.IsDir() {
if cur_dir != path {
loc := filepath.Join(target_path, cur_dir)
fmt.Printf("%s %d\n", loc, cur_size / 1024)
cur_dir = path
cur_size = 0
}
} else {
info, err := d.Info()
if err != nil { return nil }
cur_size += info.Size()
}
return nil
})
return err
}
func main() {
flag.Parse()
paths := flag.Args()
for _, path := range paths {
err := StatDir(path)
if err != nil { log.Fatal(err) }
}
}