Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: Copy() and Stdin/Stdout/Stderr #5341

Closed
gopherbot opened this issue Apr 23, 2013 · 1 comment
Closed

io: Copy() and Stdin/Stdout/Stderr #5341

gopherbot opened this issue Apr 23, 2013 · 1 comment

Comments

@gopherbot
Copy link

by rich.harkins:

What steps will reproduce the problem?
======================================
Consider the following bugged program:

-=-=- test.go -=-=-
package main;

import "io";
import "os";
import "fmt";

func main() {
    io.Copy(os.Stdin, os.Stdout);
    fmt.Printf("Got EOF -- bye\n");
}
-=-=- test.go -=-=-

What is the expected output?
============================
The /intended/ output was that input would be copied to output.  In the echo | case, the
data would be produced in stdout and the program terminated.

What do you see instead?
========================
When interactive, the program /seems/ to work.  echo |, on the other hand, hangs.

Which compiler are you using (5g, 6g, 8g, gccgo)?
=================================================
go run

Which operating system are you using?
=====================================
Linux

Which version are you using?  (run 'go version')
================================================
go version go1.0.2

Please provide any additional information below.
================================================
Now as pointed out, the program is bugged.  If you haven't figured out why, I'll give
you a sec to go back to the top and find out.

Got it?  I sure didn't.

Not until someone pointed out that my arguments to Copy() were backwards.  Coming from
UNIX, DOS, etc, the instinctive copy arguments are src -> dest.  I have no issue with
Go reversing those if that makes sense, and this is not really what this bug report is
about (though it is a little confusing to newcomers).

No, the real issue I'm raising here is that Stdin fit in the Writer hole and Stdout fit
in the Reader hole.

Looking at the docs, both Stdin and Stdout (as well as Stderr) are File's.  I can
understand why this was done, but it certainly doesn't fit right with some operations
like Copy() that depend on the interfacing to make sure the programmer isn't surprised.

I'm not sure what can be done, only that I figured that the subtlety of this bug would
make it worth consideration for the design and implementation teams.  Ideally, given
that Copy() will certainly feel odd to other newcomers besides myself (I hope I'm not
*THAT* odd), it would seem to me that Stdin should only implement Reader and
Stdout/Stderr only implement Writer.

It's not a killer problem, but it did cause some head scratching a little while ago
while trying to make a websocket command-line client work.
@robpike
Copy link
Contributor

robpike commented Apr 23, 2013

Comment 1:

Stdin is writable and stdout is readable on Unix.
$ date >&0
Tue Apr 23 11:21:52 PDT 2013
$

Status changed to Unfortunate.

@mikioh mikioh changed the title io.Copy() and Stdin/Stdout/Stderr io: Copy() and Stdin/Stdout/Stderr Jul 30, 2015
@golang golang locked and limited conversation to collaborators Aug 5, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants