Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

I am running the dup1 example from The Go Programming Language 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.

like image 362
Andrew Guo Avatar asked Mar 02 '16 17:03

Andrew Guo


3 Answers

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.

like image 59
Tony Antoniani Avatar answered Oct 23 '22 19:10

Tony Antoniani


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.

like image 32
alucic Avatar answered Oct 23 '22 18:10

alucic


For Windows, on a new line,

<Ctrl+Z><Enter>
like image 1
peterSO Avatar answered Oct 23 '22 18:10

peterSO