The Go Programming Language: example ch1/dup2 console vs file behavior


(Peter Achten) #1

Hi people,

New to Go and studying The Go Programming Language book, example chapter 1, dup2. I create a test file, test.txt with the following content:

harry
ron
hermione
ron
hagrid
dumbledore
harry
sneep
hermione

Running main.exe test.txt yields the expected output:

2       harry
2       ron
2       hermione

Running main.exe, entering the above content via the keyboard and ending with ctrl+c, typically misses one entry (not the same during several runs). Possible output:

2       ron
2       hermione

and

2       harry
2       ron

Is this expected behavior? (I’m working on Windows 10, installed go1.11.4.)

Help appreciated.


(Johan Dahl) #2

Hi. Could you show us the program?


(Lutz Horn) #3

The first chapter of GOPL is available online: http://www.gopl.io/ch1.pdf

The code for dup2 is printed on pages 10f.


(Norbert Melzer) #4

For me, if I run that program from the book linked here and Ctrl-C it, then I do see no further output at all. The program is just cancelled, and to be honest, I’d expect the same on windows.

If though I use Ctrl-D which is on linux used to send EOF, then I see the correct output every time.

As far as I remember, the windows equivalent of Ctrl-D is Ctrl-Z.


(Peter Achten) #5

Hi Norbert,

This:

answered my question, thanks!