Skip to content

Commit 246420a

Browse files
committedJan 13, 2025
refactor: replace filepath.Walk with filepath.WalkDir
filepath.WalkDir avoids unnecessary sys calls since it provides a fs.DirEntry, which includes file type information without requiring a stat call.
1 parent a91da86 commit 246420a

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed
 

‎folder/folderutil.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package folderutil
22

33
import (
4+
"io/fs"
45
"os"
56
"path/filepath"
67
"runtime"
@@ -30,11 +31,11 @@ const (
3031
// GetFiles within a folder
3132
func GetFiles(root string) ([]string, error) {
3233
var matches []string
33-
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
34+
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
3435
if err != nil {
3536
return err
3637
}
37-
if info.IsDir() {
38+
if d.IsDir() {
3839
return nil
3940
}
4041
matches = append(matches, path)
@@ -241,11 +242,11 @@ func SyncDirectory(source, destination string) error {
241242
// DedupeLinesInFiles deduplicates lines in all files in a directory
242243
// The function can be memory intensive for directories with large files.
243244
func DedupeLinesInFiles(dir string) error {
244-
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
245+
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
245246
if err != nil {
246247
return err
247248
}
248-
if !info.IsDir() {
249+
if !d.IsDir() {
249250
return fileutil.DedupeLines(path)
250251
}
251252
return nil

‎memoize/gen/generic/memoize.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ var (
2222
func main() {
2323
flag.Parse()
2424

25-
err := filepath.Walk(*src, walk)
25+
err := filepath.WalkDir(*src, walkDir)
2626
if err != nil {
2727
log.Fatal(err)
2828
}
2929
}
3030

31-
func walk(path string, info fs.FileInfo, err error) error {
32-
if info.IsDir() {
31+
func walkDir(path string, d fs.DirEntry, err error) error {
32+
if d.IsDir() {
3333
return nil
3434
}
3535

0 commit comments

Comments
 (0)