Golang:如何从终端中终止bufio Scan()函数的执行?

huangapple go评论80阅读模式
英文:

Golang: How to terminate the bufio Scan() from terminal?

问题

我正在运行《Go程序设计语言》这本书中的dup1示例(相关代码如下所示):

for input.Scan() {
    counts[input.Text()]++
}

在输入一些文本行后,我想终止Scan()方法。我尝试了Ctrl+D、Ctrl+Z和许多其他组合键,但都没有成功。只有Ctrl+C可以终止程序运行。

如何在终端中终止Scan()而不退出程序?

我在Windows 7上使用os.Stdin进行开发。

【编辑】

Ctrl+Z不起作用:

谢谢。但是对我来说这个方法不起作用:

C:\prj\src\gopl>go run dup1.go
I have tried all these combinations from the terminal
^Z
^X
^V
^B
^N

^A
^D
^F
^G

^K
^L
^Q
^W
^E
^R
^T
^Y
^U
^O
^P
2(注:只有Ctrl + C在这里起作用)

C:\prj\src\gopl>

如果我在Ubuntu上运行这个程序,只有Ctrl + D起作用,Ctrl + Z停止程序,Ctrl + C会终止程序运行。

英文:

I am running the dup1 example from [The Go Programming Language][1] book (related code shown below):

for input.Scan() {
    counts[input.Text()]++
}

After typing some lines of text, I want to terminate the Scan() method. I've tried Ctrl+D, Ctrl+Z and many other key combinations without luck. Only Ctrl+C works but that also terminates the program.

How can I terminate the Scan() from terminal without exiting the program?

I am developing on Windows 7 using os.Stdin.

[Edit]

Ctrl+Z doesn't work:

Thanks. But that doesn't work for me:

C:\prj\src\gopl\>go run dup1.go
I have tried all these combinations from the terminal
^Z
^X
^V
^B
^N

^A
^D
^F
^G

^K
^L
^Q
^W
^E
^R
^T
^Y
^U
^O
^P
2(Notes: only Ctrl + C works here)

C:\prj\src\gopl\>

If I run this program in Ubuntu, only Ctrl + D works, Ctrl + Z will Stop the program and Ctrl + C will terminate it.
[1]: http://www.gopl.io/

答案1

得分: 10

没有人问,但我在OSX上遇到了这个问题,并在搜索中找到了解决方法。Mac OSX终端在按下控制-D时会将其识别为EOF,而且要在行的开头按下。

英文:

No one asked but I had this problem in OSX and this came up in a search. Mac OSX terminals recognize EOF when control-D is pressed at the beginning of a line.

答案2

得分: 4

ctrl + shift + d 是在新行中的第一个字符时,将EOF发送到终端。

运行以下命令:

go run main.go

然后输入以下内容:

> line one
> another line
> another line
> ctrl + shift + d

你应该会看到输出结果:

2 another line

根据这个评论中的描述 https://stackoverflow.com/a/21658005/1522019 ,你可以使用 stty all 命令找到EOF组合键。我一直在使用 ctrl + d,在我看来它和 ctrl + D 是一样的。

英文:

ctrl + shift + d will send EOF to the terminal if it's the first character in a new line.

go run main.go

> line one
> another line
> another line
> ctrl + shift + d

And you should see the output

2 another line

As described in this comment https://stackoverflow.com/a/21658005/1522019 .
You can use stty all to find EOF combination. I kept doing ctrl + d which in my mind is the same as ctrl + D.

答案3

得分: 1

对于Windows系统,在新的一行上,输入以下内容:

<Ctrl+Z><Enter>
英文:

For Windows, on a new line,

&lt;Ctrl+Z&gt;&lt;Enter&gt;

huangapple
  • 本文由 发表于 2016年3月3日 01:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/35754563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定