Skip to content

Commit

Permalink
fix: fix Cat function which can't read text files line by line
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYihang committed Jan 16, 2024
1 parent 2a4791c commit 2a8a7ac
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions gojob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gojob

import (
"bufio"
"bytes"
"log"
"log/slog"
"os"
"sync"
Expand Down Expand Up @@ -82,22 +82,31 @@ func Tail[T interface{}](in chan T, max int) chan T {
}

// Cat takes a file path and returns a channel with the lines of the file
// each line is trimmed of whitespace
func Cat(path string) chan []byte {
out := make(chan []byte)
func Cat(filePath string) <-chan string {
out := make(chan string)

go func() {
defer close(out)
fd, err := os.Open(path)
defer close(out) // Ensure the channel is closed when the goroutine finishes

// Open the file
file, err := os.Open(filePath)
if err != nil {
slog.Error("error occured while opening file", slog.String("path", path), slog.String("error", err.Error()))
return
log.Printf("Error opening file: %v", err)
return // Close the channel and exit the goroutine
}
defer fd.Close()
scanner := bufio.NewScanner(fd)
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
out <- bytes.TrimSpace(scanner.Bytes())
out <- scanner.Text() // Send the line to the channel
}

// Check for errors during Scan, excluding EOF
if err := scanner.Err(); err != nil {
log.Printf("Error reading file: %v", err)
}
}()

return out
}

Expand Down

0 comments on commit 2a8a7ac

Please sign in to comment.