add: error handling to scanner example
This commit is contained in:
@ -17,6 +17,9 @@ func main() {
|
|||||||
// Create a new scanner that scans from the standard-input
|
// Create a new scanner that scans from the standard-input
|
||||||
in := bufio.NewScanner(os.Stdin)
|
in := bufio.NewScanner(os.Stdin)
|
||||||
|
|
||||||
|
// Simulate an error
|
||||||
|
// os.Stdin.Close()
|
||||||
|
|
||||||
// Stores the total number of lines in the input
|
// Stores the total number of lines in the input
|
||||||
var lines int
|
var lines int
|
||||||
|
|
||||||
@ -27,6 +30,12 @@ func main() {
|
|||||||
// Get the current line from the scanner's buffer
|
// Get the current line from the scanner's buffer
|
||||||
// fmt.Println("Scanned Text :", in.Text())
|
// fmt.Println("Scanned Text :", in.Text())
|
||||||
// fmt.Println("Scanned Bytes:", in.Bytes())
|
// fmt.Println("Scanned Bytes:", in.Bytes())
|
||||||
|
in.Text()
|
||||||
}
|
}
|
||||||
fmt.Printf("There %d lines.\n", lines)
|
fmt.Printf("There %d lines.\n", lines)
|
||||||
|
|
||||||
|
err := in.Err()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("ERROR:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,4 +70,9 @@ func main() {
|
|||||||
|
|
||||||
// Print the total visits for all domains
|
// Print the total visits for all domains
|
||||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", total)
|
fmt.Printf("\n%-30s %10d\n", "TOTAL", total)
|
||||||
|
|
||||||
|
// Let's handle the error
|
||||||
|
if err := in.Err(); err != nil {
|
||||||
|
fmt.Println("> Err:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,4 +86,9 @@ func main() {
|
|||||||
|
|
||||||
// Print the total visits for all domains
|
// Print the total visits for all domains
|
||||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
||||||
|
|
||||||
|
// Let's handle the error
|
||||||
|
if err := in.Err(); err != nil {
|
||||||
|
fmt.Println("> Err:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,4 +59,9 @@ func main() {
|
|||||||
|
|
||||||
// Print the total visits for all domains
|
// Print the total visits for all domains
|
||||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
||||||
|
|
||||||
|
// Let's handle the error
|
||||||
|
if err := in.Err(); err != nil {
|
||||||
|
fmt.Println("> Err:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,4 +45,9 @@ func main() {
|
|||||||
|
|
||||||
// Print the total visits for all domains
|
// Print the total visits for all domains
|
||||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
||||||
|
|
||||||
|
// Let's handle the error
|
||||||
|
if err := in.Err(); err != nil {
|
||||||
|
fmt.Println("> Err:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,10 @@ func main() {
|
|||||||
fmt.Printf("\n%-25s -> %d\n", "TOTAL", p.total)
|
fmt.Printf("\n%-25s -> %d\n", "TOTAL", p.total)
|
||||||
|
|
||||||
if p.lerr != nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,10 @@ func main() {
|
|||||||
// // Print the total visits for all domains
|
// // Print the total visits for all domains
|
||||||
// fmt.Printf("\n%-30s %10d\n", "TOTAL", p.Total)
|
// fmt.Printf("\n%-30s %10d\n", "TOTAL", p.Total)
|
||||||
|
|
||||||
|
// if err := in.Err(); err != nil {
|
||||||
|
// fmt.Println("> Err:", err)
|
||||||
|
// }
|
||||||
|
|
||||||
s, _ := json.MarshalIndent(p, "", "\t")
|
s, _ := json.MarshalIndent(p, "", "\t")
|
||||||
fmt.Println(string(s))
|
fmt.Println(string(s))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user