From 28903ad6c86247eb872076bb0925aa1d904b706b Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Sat, 27 Apr 2019 10:45:47 +0300 Subject: [PATCH] add: error handling to scanner example --- 23-project-log-parser/01-scanning/main.go | 9 +++++++++ 23-project-log-parser/02-project-log-parser/main.go | 5 +++++ 24-structs/05-project-log-parser-structs/main.go | 5 +++++ 25-functions-and-pointers/03-refactor-to-funcs/main.go | 5 +++++ .../04-pass-by-value-semantics/main.go | 5 +++++ 25-functions-and-pointers/08-log-parser-pointers/main.go | 6 +++++- x-tba/log-parser-json/main.go | 4 ++++ 7 files changed, 38 insertions(+), 1 deletion(-) diff --git a/23-project-log-parser/01-scanning/main.go b/23-project-log-parser/01-scanning/main.go index 187ecbd..0f49476 100644 --- a/23-project-log-parser/01-scanning/main.go +++ b/23-project-log-parser/01-scanning/main.go @@ -17,6 +17,9 @@ func main() { // Create a new scanner that scans from the standard-input in := bufio.NewScanner(os.Stdin) + // Simulate an error + // os.Stdin.Close() + // Stores the total number of lines in the input var lines int @@ -27,6 +30,12 @@ func main() { // Get the current line from the scanner's buffer // fmt.Println("Scanned Text :", in.Text()) // fmt.Println("Scanned Bytes:", in.Bytes()) + in.Text() } fmt.Printf("There %d lines.\n", lines) + + err := in.Err() + if err != nil { + fmt.Println("ERROR:", err) + } } diff --git a/23-project-log-parser/02-project-log-parser/main.go b/23-project-log-parser/02-project-log-parser/main.go index 5ffc2d4..1fb6738 100644 --- a/23-project-log-parser/02-project-log-parser/main.go +++ b/23-project-log-parser/02-project-log-parser/main.go @@ -70,4 +70,9 @@ func main() { // Print the total visits for all domains fmt.Printf("\n%-30s %10d\n", "TOTAL", total) + + // Let's handle the error + if err := in.Err(); err != nil { + fmt.Println("> Err:", err) + } } diff --git a/24-structs/05-project-log-parser-structs/main.go b/24-structs/05-project-log-parser-structs/main.go index cd3394f..b744ad4 100644 --- a/24-structs/05-project-log-parser-structs/main.go +++ b/24-structs/05-project-log-parser-structs/main.go @@ -86,4 +86,9 @@ func main() { // Print the total visits for all domains fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total) + + // Let's handle the error + if err := in.Err(); err != nil { + fmt.Println("> Err:", err) + } } diff --git a/25-functions-and-pointers/03-refactor-to-funcs/main.go b/25-functions-and-pointers/03-refactor-to-funcs/main.go index a669711..00ca604 100644 --- a/25-functions-and-pointers/03-refactor-to-funcs/main.go +++ b/25-functions-and-pointers/03-refactor-to-funcs/main.go @@ -59,4 +59,9 @@ func main() { // Print the total visits for all domains fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total) + + // Let's handle the error + if err := in.Err(); err != nil { + fmt.Println("> Err:", err) + } } diff --git a/25-functions-and-pointers/04-pass-by-value-semantics/main.go b/25-functions-and-pointers/04-pass-by-value-semantics/main.go index d63490d..7f1e1fe 100644 --- a/25-functions-and-pointers/04-pass-by-value-semantics/main.go +++ b/25-functions-and-pointers/04-pass-by-value-semantics/main.go @@ -45,4 +45,9 @@ func main() { // Print the total visits for all domains fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total) + + // Let's handle the error + if err := in.Err(); err != nil { + fmt.Println("> Err:", err) + } } diff --git a/25-functions-and-pointers/08-log-parser-pointers/main.go b/25-functions-and-pointers/08-log-parser-pointers/main.go index f24d393..a07dfb3 100644 --- a/25-functions-and-pointers/08-log-parser-pointers/main.go +++ b/25-functions-and-pointers/08-log-parser-pointers/main.go @@ -31,6 +31,10 @@ func main() { fmt.Printf("\n%-25s -> %d\n", "TOTAL", p.total) if p.lerr != nil { - fmt.Printf("> Err: %s\n", p.lerr) + fmt.Println("> Err:", p.lerr) + } + + if err := in.Err(); err != nil { + fmt.Println("> Err:", err) } } diff --git a/x-tba/log-parser-json/main.go b/x-tba/log-parser-json/main.go index b85b8a8..903cf83 100644 --- a/x-tba/log-parser-json/main.go +++ b/x-tba/log-parser-json/main.go @@ -88,6 +88,10 @@ func main() { // // Print the total visits for all domains // fmt.Printf("\n%-30s %10d\n", "TOTAL", p.Total) + // if err := in.Err(); err != nil { + // fmt.Println("> Err:", err) + // } + s, _ := json.MarshalIndent(p, "", "\t") fmt.Println(string(s)) }