如何摆脱scanner.scan()for循环? [重复]

This question already has an answer here:

When I use "bufio" package, the standard code is just like:

input := bufio.NewScanner(os.Stdin)
for input.Scan() {
    // xxxxx
}

When I run the program, the for-loop can't stop whatever I input. I have tried newline, space, ctrl-d, ctrl-z. According to the document, a blank newline should be able to stop the program.

The program is running under Windows 7 CMD environment, or mingw-bash.

Thanks.

</div>

You may input some specific string as a signal to stop the loop. In the below example, whenever "quit" is entered, the loop breaks.

package main

import (
    "bufio"
    "os"
)

func main() {
    input := bufio.NewScanner(os.Stdin)
    for input.Scan() {
        indata := input.Text()
        if indata == "quit" {
            break
        }
    }
}