I have this simple code to read all input from the console:
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
if input.Text() == "end" { break }
fmt.Println(input.Text())
}
The code is how it works. What I want to do is get rid of the if clause. In my understanding of the documentation, if the line is empty, it input.Scan()should return false and, therefore, exit the loop.
The scan advances the scanner to the next token, which will then be available through the Bytes or Text method. It returns false when scanning stops, either reaching the end of the input, or an error. After Scan returns false, the Err method will return any error that occurred during the scan, except that if it was io.EOF, Err will return zero. Panic scan if the split function returns 100 empty tokens without advancing the input. This is a common error mode for scanners.
I misinterpreted the documentation, and is it really necessary that such an if-break condition breaks out? (I am using Go 1.5.2, starting the program using "go run".)
Chris source
share