-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.go
55 lines (44 loc) · 987 Bytes
/
count.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
count.go counts the number of files in given directories.
Since one file contains one problem, the program's output can also
tell how many questions that we have completed so far.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"text/tabwriter"
)
func main() {
// format in tab-separated columns with a tab stop of 8.
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
total := 0
directories := []string{"ctci", "gtci", "interviewcake", "leetcode", "other"}
for _, dir := range directories {
n := countFiles(dir)
fmt.Fprintf(w, "%v\t%v\n", dir, n)
total += n
}
fmt.Fprintf(w, "-------------\t--\n")
fmt.Fprintf(w, "total\t%v\n", total)
w.Flush()
}
func countFiles(dir string) int {
counter := 0
err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if dir != path {
counter++
}
return nil
})
if err != nil {
fmt.Println(err)
}
return counter
}