英文:
Break out of input.Scan()
问题
我有这段简单的代码来从控制台读取所有输入:
input := bufio.NewScanner(os.Stdin) // 创建一个从控制台读取输入的扫描器
for input.Scan() {
if input.Text() == "end" { break } // 当用户输入单词 "end" 时,跳出输入循环
fmt.Println(input.Text())
}
这段代码目前可以正常工作。我想做的是摆脱这个 if 语句。根据我对文档的理解,如果一行为空,input.Scan()
应该返回 false,从而跳出循环。
Scan 方法将扫描器推进到下一个标记,然后可以通过 Bytes 或 Text 方法获取该标记。当扫描停止时,它返回 false,无论是到达输入的末尾还是出现错误。在 Scan 返回 false 后,Err 方法将返回扫描过程中发生的任何错误,但如果是
io.EOF
,Err 将返回 nil。如果分割函数返回 100 个空标记而不推进输入,则 Scan 会引发 panic。这是扫描器的常见错误模式。
我是否误解了文档,实际上还是需要这样的 if 语句来跳出循环?(我使用的是 Go 1.5.2,并使用 "go run" 运行程序。)
英文:
I have this simple code to read all input from the console:
input := bufio.NewScanner(os.Stdin) //Creating a Scanner that will read the input from the console
for input.Scan() {
if input.Text() == "end" { break } //Break out of input loop when the user types the word "end"
fmt.Println(input.Text())
}
The code as it is works. What I want to do is get rid of the if-clause. In my understanding of the documentation if a line is empty input.Scan()
should return false and therefore break out of the loop.
> Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method. It returns false when the scan stops, either by reaching the end of the input or an error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF
, Err will return nil. Scan panics if the split function returns 100 empty tokens without advancing the input. This is a common error mode for scanners.
Am I misinterpreting the documentation and it is actually necessary to have such a if-clause to break out? (I'm using Go 1.5.2 running the program using "go run".)
答案1
得分: 41
我认为你误读了文档。默认的扫描器是ScanLines函数。
文档中写道:
ScanLines是Scanner的一个分割函数,它返回文本的每一行,去除任何尾随的换行符。返回的行可能为空。换行符是一个可选的回车符,后面跟着一个必需的换行符。在正则表达式表示中,它是
\r?\n
。即使最后一行没有换行符,也会返回输入的最后一个非空行。
这里有两个重要的点:
- 返回的行可能为空:这意味着它会返回空行。
- 即使最后一行没有换行符,也会返回输入的最后一个非空行:这意味着如果文件的最后一行非空,它总是会返回该行。但这并不意味着空行会结束扫描器。
扫描器会在EOF(文件结束)时停止。例如,输入Ctrl-D将发送文件结束信号并停止扫描器。
英文:
I think you misread the documentation. The default scanner is ScanLines function.
Documentation says:
> ScanLines is a split function for a Scanner that returns each line of
> text, stripped of any trailing end-of-line marker. The returned line
> may be empty. The end-of-line marker is one optional carriage return
> followed by one mandatory newline. In regular expression notation, it
> is \r?\n
. The last non-empty line of input will be returned even if
> it has no newline.
Two important points here:
- The return line may be empty: It means it returns empty lines.
- The last non-empty line of input will be returned even if it has no newline: It means the last line of a file is always returned if it is non empty. It does not mean however an empty line end the stream.
The scanner will stop on EOF (End Of File). Typing Ctrl-D for example will send end of file and stop the scanner.
答案2
得分: 8
输入一个空白的新行不会自动停止扫描器。
如果它没有出错,就不要修复它 - 但是你可以使其按照你的要求工作。这不会消除你的 if
代码块,但会按照你期望的方式工作,即按下回车键而没有输入将停止扫描器:
input := bufio.NewScanner(os.Stdin) // 创建一个从控制台读取输入的扫描器
for input.Scan() {
if input.Text() == "" {
break
}
fmt.Println(input.Text())
}
英文:
Typing a blank new line will not automatically stop the scanner.
If it ain't broke, don't fix it--but you can make it behave as you require. This doesn't get rid of your if
block but functions as you expected scanner to, i.e. hitting enter with no input will stop the scanner:
input := bufio.NewScanner(os.Stdin) //Creating a Scanner that will read the input from the console
for input.Scan() {
if input.Text() == "" {
break
}
fmt.Println(input.Text())
}
答案3
得分: 5
CTRL+D
用于中断操作,
如果你想要轻松输入数据,可以使用cat input.txt | go run script.go
或者go run script.go < input.txt
。
英文:
CTRL+D
to break,
if you want to input data easily, you can use cat input.txt | go run script.go
or go run script.go < input.txt
.
答案4
得分: 1
你没有误解文档。
>当扫描停止时,要么是到达输入的末尾,要么是出现错误,它会返回false。
文档中的说明是正确的。但是你忽略了你需要一种方法来提供输入的结束,即来自控制台的EOF。
在Linux中,你可以按下"CTRL+D",这会从终端发送EOF信号。
不过这里有一个小问题,"CTRL+D"只在行的开头起作用。因此,要终止终端上的输入,你需要换到新的一行,并在该行的开头按下"CTRL+D"作为第一个输入。
英文:
You are not misinterpreting the documentation.
>It returns false when the scan stops, either by reaching the end of the input or an error.
What documentation states is correct. But you are missing that you need a way to provide end of the input i.e. EOF from the console.
In linux you can press "CTRL+D", which signals EOF from terminal.
Though there is a small catch here, that "CTRL+D" works only at the beginning of the line. Thus to terminate the input from terminal you need to go to a new line and press "CTRL+D" as first input on the line.
答案5
得分: 0
对于Mac系统,您可以尝试使用Ctrl + D
来默认中断。如果您在行尾,您需要按下Ctrl + D
两次。
英文:
For Mac systems, you can try Ctrl + D
to break by default. If you are at the end of the line you will have to hit Ctrl + D
2 times.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论