-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (66 loc) · 1.27 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"os"
"log"
"sync"
"fmt"
"io/ioutil"
)
func main() {
dirList(os.Args[1])
}
func dirList(path string) {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
var floatSize float64
var sizeUnit string
var fileName string
var filePath string
for _, file := range files {
fileName = file.Name()
filePath = path + fileName
if (!file.IsDir()) {
floatSize, sizeUnit = getFileSize(file.Size())
} else {
var size, _= dirSize(filePath)
floatSize, sizeUnit = getFileSize(size)
}
fmt.Printf("%.1f%s\t%s\n", floatSize, sizeUnit, filePath)
}
}
func dirSize(path string) (int64, error) {
var mu sync.Mutex
var size int64
err := fastWalk(path, func(path string, typ os.FileMode) error {
mu.Lock()
defer mu.Unlock()
if !typ.IsDir() {
var f, err = os.Lstat(path)
if err != nil {
return err
}
size += f.Size()
}
return nil
})
return size, err
}
func getFileSize(size int64)(float64, string){
var floatSize = float64(size)
var sizeUnit = "B"
if(floatSize > 1024) {
floatSize = floatSize / 1024
sizeUnit = "K"
}
if(floatSize > 1024) {
floatSize = floatSize / 1024
sizeUnit = "M"
}
if(floatSize > 1024) {
floatSize = floatSize / 1024
sizeUnit = "G"
}
return floatSize, sizeUnit
}