From 389685c675e704179d6dda760bacc690c65c318d Mon Sep 17 00:00:00 2001 From: Jonathan Hecl Date: Sun, 13 Feb 2022 02:48:39 -0300 Subject: [PATCH] Fix buffer on ReadFile --- goini.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/goini.go b/goini.go index 4d3a1b4..c9517b7 100644 --- a/goini.go +++ b/goini.go @@ -94,10 +94,12 @@ func ReadFile(Path string) ([]string, error) { return nil, err } defer f.Close() - var lines []string - buf := make([]byte, 32*1024) + var ( + buf []byte = make([]byte, 32*1024) + lines []string + line []byte = []byte{} + ) for { - line := []byte{} n, err := f.Read(buf) if n > 0 { for i := 0; i < n; i++ { @@ -111,9 +113,6 @@ func ReadFile(Path string) ([]string, error) { line = append(line, buf[i]) } } - if len(line) > 0 { - lines = append(lines, string(line)) - } } if err == io.EOF { break @@ -122,6 +121,9 @@ func ReadFile(Path string) ([]string, error) { return nil, fmt.Errorf("read %d bytes: %v", n, err) } } + if len(line) > 0 { + lines = append(lines, string(line)) + } return lines, nil }