From 9c7d65b5bc22a24c148d61d353494009471c71f2 Mon Sep 17 00:00:00 2001 From: xielizyh <33270435+xielizyh@users.noreply.github.com> Date: Tue, 2 Mar 2021 01:28:04 +0800 Subject: [PATCH] fix read_csv.go can not read last line (#787) --- eBook/exercises/chapter_12/read_csv.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/eBook/exercises/chapter_12/read_csv.go b/eBook/exercises/chapter_12/read_csv.go index 266e276..84d5485 100755 --- a/eBook/exercises/chapter_12/read_csv.go +++ b/eBook/exercises/chapter_12/read_csv.go @@ -29,9 +29,7 @@ func main() { for { // read one line from the file: line, err := reader.ReadString('\n') - if err == io.EOF { - break - } + readErr := err // remove \r and \n so 2(in Windows, in Linux only \n, so 1): line = string(line[:len(line)-2]) //fmt.Printf("The input was: -%s-", line) @@ -53,6 +51,9 @@ func main() { } else { bks = append(bks, *book) } + if readErr == io.EOF { + break + } } fmt.Println("We have read the following books from the file: ") for _, bk := range bks { @@ -65,4 +66,5 @@ We have read the following books from the file: {"The ABC of Go" 25.5 1500} {"Functional Programming with Go" 56 280} {"Go for It" 45.900001525878906 356} +{"The Go Way" 55 5} */