From 7116642758022fdf5dda20c16c91d0a7e093c117 Mon Sep 17 00:00:00 2001 From: Taro FURUKAWA <6879289+0tarof@users.noreply.github.com> Date: Sun, 2 Jan 2022 17:24:11 +0900 Subject: [PATCH] use bufio.Reader instead of bufio.Scanner (#79) * use bufio.Reader instead of bufio.Scanner * review fix --- internal/util/files.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/internal/util/files.go b/internal/util/files.go index e0a1cd6..3c166da 100644 --- a/internal/util/files.go +++ b/internal/util/files.go @@ -2,9 +2,10 @@ package util import ( "bufio" + "bytes" "fmt" + "io" "io/ioutil" - "log" "os" "path/filepath" ) @@ -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 }