jkh
This commit is contained in:
parent
389dcbc366
commit
dec7db5f5e
3
book_with_bridge/du/go.mod
Normal file
3
book_with_bridge/du/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module main
|
||||
|
||||
go 1.25.5
|
||||
58
book_with_bridge/du/main.go
Normal file
58
book_with_bridge/du/main.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
roots := flag.Args()
|
||||
if len(roots) == 0 {
|
||||
roots = []string{"."}
|
||||
}
|
||||
|
||||
fileSizes := make(chan int64)
|
||||
|
||||
go func() {
|
||||
for _,root := range roots {
|
||||
WalkDir(root,fileSizes)
|
||||
}
|
||||
close(fileSizes)
|
||||
}()
|
||||
|
||||
var nfiles,nbytes int64
|
||||
for size := range fileSizes {
|
||||
nfiles++
|
||||
nbytes += size
|
||||
}
|
||||
printDU(nfiles,nbytes)
|
||||
|
||||
}
|
||||
|
||||
func printDU(nfiles, nbytes int64) {
|
||||
fmt.Printf("%d files, %.1f GB\n",nfiles,float64(nbytes)/1e9)
|
||||
}
|
||||
|
||||
func WalkDir(dir string, fileSizes chan <- int64) {
|
||||
for _, entry := range dirents(dir) {
|
||||
if entry.IsDir() {
|
||||
subdir := filepath.Join(dir,entry.Name())
|
||||
WalkDir(subdir,fileSizes)
|
||||
} else {
|
||||
size,_ := entry.Info()
|
||||
fileSizes <- size.Size()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func dirents(dir string) []os.DirEntry{
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr,"du1: %v\n",err)
|
||||
return nil
|
||||
}
|
||||
return entries
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user