MCPcopy
hub / github.com/adonovan/gopl.io / main

Function main

ch8/du3/main.go:26–75  ·  view source on GitHub ↗

!+

()

Source from the content-addressed store, hash-verified

24
25//!+
26func main() {
27 // ...determine roots...
28
29 //!-
30 flag.Parse()
31
32 // Determine the initial directories.
33 roots := flag.Args()
34 if len(roots) == 0 {
35 roots = []string{"."}
36 }
37
38 //!+
39 // Traverse each root of the file tree in parallel.
40 fileSizes := make(chan int64)
41 var n sync.WaitGroup
42 for _, root := range roots {
43 n.Add(1)
44 go walkDir(root, &n, fileSizes)
45 }
46 go func() {
47 n.Wait()
48 close(fileSizes)
49 }()
50 //!-
51
52 // Print the results periodically.
53 var tick <-chan time.Time
54 if *vFlag {
55 tick = time.Tick(500 * time.Millisecond)
56 }
57 var nfiles, nbytes int64
58loop:
59 for {
60 select {
61 case size, ok := <-fileSizes:
62 if !ok {
63 break loop // fileSizes was closed
64 }
65 nfiles++
66 nbytes += size
67 case <-tick:
68 printDiskUsage(nfiles, nbytes)
69 }
70 }
71
72 printDiskUsage(nfiles, nbytes) // final totals
73 //!+
74 // ...select loop...
75}
76
77//!-
78

Callers

nothing calls this directly

Calls 3

AddMethod · 0.80
walkDirFunction · 0.70
printDiskUsageFunction · 0.70

Tested by

no test coverage detected