Andrew Guo
Andrew Guo

Reputation: 101

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

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.

Upvotes: 10

Views: 2635

Answers (3)

alucic
alucic

Reputation: 13293

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.

Upvotes: 5

Tony Antoniani
Tony Antoniani

Reputation: 113

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.

Upvotes: 11

peterSO
peterSO

Reputation: 166529

For Windows, on a new line,

<Ctrl+Z><Enter>

Upvotes: 2

Related Questions