突破input.Scan() [英] Break out of input.Scan()

查看:146
本文介绍了突破input.Scan()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  input:= bufio.NewScanner(os.Stdin)

我有这个简单的代码来读取控制台中的所有输入: )//创建一个将从控制台读取输入的扫描器

用于input.Scan(){
如果input.Text()==end{break} // Break当用户键入单词结束时输出循环不在输出循环中
fmt.Println(input.Text())
}

代码正如它的工作原理。我想要做的是摆脱if-从句。在我对文档的理解中,如果一行是空的 input.Scan()应该返回false并因此跳出循环。


扫描将扫描仪推进到下一个标记,然后通过字节或文本方法使用该标记。当扫描停止时,它会返回false,通过到达输入末尾或错误。在Scan返回false后,Err方法将返回扫描期间发生的任何错误,但是如果它是 io.EOF ,则Err将返回nil。如果分割函数返回100个空令牌而不提前输入,则扫描恐慌。这是扫描仪常见的错误模式。


我误解了文档,实际上有必要有这样一个if-爆发? (我正在使用Go 1.5.2,使用go run运行程序。)

解决方案

我认为你误读了文档。默认扫描仪是ScanLines功能。



文档说明:


ScanLines是一款分离扫描仪的功能,返回
文本的每一行,删除任何尾随行尾标记。返回的
行可能为空。行尾标记是一个可选的回车符
,后跟一个强制换行符。在正则表达式中,
\r?\\\
。即使
没有换行,也会返回最后一个非空行输入。


这里有两点重要:


  • 返回行可能为空:这意味着它返回空行。 b
  • 即使没有换行符,也会返回最后一个非空输入行:这意味着文件的最后一行总是返回,如果它不为空。这并不意味着空行会结束流。



扫描程序将在EOF(文件结束)处停止。例如,键入Ctrl-D将发送文件结尾并停止扫描程序。


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".)

解决方案

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.

这篇关于突破input.Scan()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆