Skip to content

Commit a317c5d

Browse files
committed
docs: Adding function comments
1 parent 5bbb585 commit a317c5d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ A file finder written in Go.
44
Uses a worker pool to search through directory trees quicker by taking advantage of multi-core machines.
55
Go concurrency allows it to run on single core machines.
66

7+
Each worker reads all of the items in a single directory.
8+
If one of the items is itself a directory, it's added to the work queue and
9+
another worker will begin searching that directory.
10+
711
### Features
812
- Concurrency search directories for desired pattern
913
- Exit early with a max results option

search.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func showResults(ch chan string, limit *int) {
2626
}
2727
}
2828

29+
// dirChecker check how many directories are being searched
30+
// once the number of directories hits zero, then all files & dirs have been
31+
// checked and the program can be closed
2932
func dirChecker(in chan int, work chan string) {
3033
n := 1
3134
for i := range in {
@@ -37,6 +40,7 @@ func dirChecker(in chan int, work chan string) {
3740
}
3841
}
3942

43+
// createWorkerPool create all workers and close our results channel after they stop searching
4044
func createWorkerPool(p string, in chan string, failover chan string, results chan string, cnt chan int, w int) {
4145
var wg sync.WaitGroup
4246
for i := 0; i < w; i++ {
@@ -46,16 +50,17 @@ func createWorkerPool(p string, in chan string, failover chan string, results ch
4650
search(p, in, failover, results, cnt)
4751
}()
4852
}
53+
4954
wg.Wait()
5055
close(results)
5156
}
5257

58+
// search looks through files and directories for a given substring
5359
func search(pattern string, in chan string, failover chan string, out chan string, cnt chan int) {
5460
for path := range in {
5561
items, err := os.ReadDir(path)
5662
if err != nil {
57-
fmt.Println("Error reading the directory", path)
58-
fmt.Println(err)
63+
slog.Error("failed to reading the directory", "path", path, "err", err)
5964
cnt <- -1
6065
}
6166
ItemSearch:
@@ -74,6 +79,7 @@ func search(pattern string, in chan string, failover chan string, out chan strin
7479
case failover <- subPath:
7580
}
7681
}
82+
7783
//Always check if the name of the thing matches pattern, including directory names
7884
if strings.Contains(item.Name(), pattern) {
7985
//subPath is repeated but no point in creating an allocation if not required

0 commit comments

Comments
 (0)