Skip to content

Commit

Permalink
use bufio.Reader instead of bufio.Scanner (#79)
Browse files Browse the repository at this point in the history
* use bufio.Reader instead of bufio.Scanner

* review fix
  • Loading branch information
0tarof authored Jan 2, 2022
1 parent 604b4c4 commit 7116642
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions internal/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package util

import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -43,15 +44,26 @@ func ReadALine(filename string) (string, error) {
return "", fmt.Errorf("error to open the file: %w", err)
}

var line string
fileScanner := bufio.NewScanner(file)
for fileScanner.Scan() {
line = fileScanner.Text()
break
r := bufio.NewReader(file)

data, cont, err := r.ReadLine()
if err != nil && err != io.EOF {
return "", fmt.Errorf("error to read the file: %w", err)
}
if err := fileScanner.Err(); err != nil {
log.Fatalf("error to scan the file: %s", err)

buf := bytes.NewBuffer(data)
for cont {
data, cont, err = r.ReadLine()
if err == io.EOF {
break
} else if err != nil {
return "", fmt.Errorf("error to read the file: %w", err)
}

if _, err = buf.Write(data); err != nil {
return "", fmt.Errorf("error to append data to buffer: %w", err)
}
}

return line, nil
return buf.String(), nil
}

0 comments on commit 7116642

Please sign in to comment.