add: error handling to scanner example

This commit is contained in:
Inanc Gumus
2019-04-27 10:45:47 +03:00
parent 8cff19f314
commit 28903ad6c8
7 changed files with 38 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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))
}