What's the best way to stop the below scanning?
outputReader := io.MultiReader(outReader, errReader)
scanner := bufio.NewScanner(outputReader)
for scanner.Scan(){
scanner.Text():
}
If you are stuck in continuous loop and hitting ENTER
wont help in exiting the loop of scanner.Scan()
, then try ctrl+Z
to exit loop.
Below is an example code for taking input from console and printing the duplicate lines.
func main() {
counts := make(map[string]int)
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Accepting inputs")
for scanner.Scan() {
counts[input.Text()]++
}
// NOTE: ignoring potential errors from input.Err()
fmt.Println("Printing output")
for line, n := range counts {
if n > 1 {
fmt.Printf("Count: %d\t Line: %s\n", n, line)
}
}
}
Console Output:
Accepting inputs
Hi
hello
hi
Hi
^Z
Printing output
Count: 2 Line: Hi
We can see that just after pressing ctrl+Z
the scanner.Scan() exited the loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With