Skip to content

Commit 7d2b081

Browse files
committed
feat: add file utils
1 parent 2bae1f1 commit 7d2b081

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

utils/file.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package utils
2+
3+
import (
4+
"io/fs"
5+
"path/filepath"
6+
)
7+
8+
// DirSize 获取一个目录的大小
9+
func DirSize(dirPath string) (int64, error) {
10+
var size int64
11+
err := filepath.Walk(dirPath, func(path string, info fs.FileInfo, err error) error {
12+
if err != nil {
13+
return err
14+
}
15+
if !info.IsDir() {
16+
size += info.Size()
17+
}
18+
return nil
19+
})
20+
return size, err
21+
}

utils/file_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package utils
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestDirSize(t *testing.T) {
10+
dir, _ := os.Getwd()
11+
dirSize, err := DirSize(dir)
12+
assert.Nil(t, err)
13+
assert.True(t, dirSize > 0)
14+
}

0 commit comments

Comments
 (0)