FAQ
os.Stderr fails in Windows system at using it into a test:

package main

import (
"bufio"
"fmt"
"log"
"os"
"testing"
)

func Test(t *testing.T) {
buf := bufio.NewReader(os.Stderr)
fmt.Print(" Write (enter to finish): ")
line, err := buf.ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Println(string(line))
}

=== RUN Test
Write (enter to finish): 2012/09/06 15:12:42 read /dev/stderr: The handle
is invalid.
exit status 1
FAIL github.com/arios 0.062s

Search Discussions

  • Jan Mercl at Sep 6, 2012 at 1:34 pm

    On Thu, Sep 6, 2012 at 3:18 PM, Archos wrote:
    os.Stderr fails in Windows system at using it into a test:
    I can confirm the code doesn't fail on Linux/64. Can you please share
    why you're *reading* from stderr? I'm surprised it's even possible[*],
    my bad.

    -j

    [*] I still somehow think stderr == line printer (originally).
  • Daniel Morsing at Sep 6, 2012 at 1:51 pm

    On Thu, Sep 6, 2012 at 3:18 PM, Archos wrote:
    ...
    buf := bufio.NewReader(os.Stderr)
    ...
    os.Stderr is write only. No read from it will ever succeed.

    Regards,
    Daniel Morsing
  • Archos at Sep 6, 2012 at 1:36 pm
    El jueves, 6 de septiembre de 2012 14:26:01 UTC+1, Daniel Morsing escribió:
    On Thu, Sep 6, 2012 at 3:18 PM, Archos <raul...@sent.com <javascript:>>
    wrote:
    ...
    buf := bufio.NewReader(os.Stderr)
    ...
    os.Stderr is write only. No read from it will ever succeed.
    The "go test" tool runs tests with standard input connected to standard
    error.
    If you have a system different to Windows, you could check that it works.
  • Minux at Sep 6, 2012 at 1:38 pm

    On Thu, Sep 6, 2012 at 9:29 PM, Archos wrote:
    El jueves, 6 de septiembre de 2012 14:26:01 UTC+1, Daniel Morsing escribió:
    On Thu, Sep 6, 2012 at 3:18 PM, Archos wrote:
    ...
    buf := bufio.NewReader(os.Stderr)
    ...
    os.Stderr is write only. No read from it will ever succeed.
    The "go test" tool runs tests with standard input connected to standard
    error.
    If you have a system different to Windows, you could check that it works.
    Right if you run test binary from a tty. That's because the shell usually
    dup fd 0
    to 1 and 2, so stdout and stderr actually is a tty opened read/write.

    i think 'go test' connect stderr to stdin is just to avoid the test binary
    from reading
    from stdin which will make the test dependent on user input, perhaps it
    could just
    close it instead.
  • Minux at Sep 6, 2012 at 1:48 pm

    On Thu, Sep 6, 2012 at 9:37 PM, minux wrote:

    Right if you run test binary from a tty. That's because the shell usually
    dup fd 0
    to 1 and 2, so stdout and stderr actually is a tty opened read/write.
    Correction: technically, it's not the shell that duped the fds, it should
    be getty(1)
    or whatever (maybe indirectly) starts your shell (e.g. terminal emulator).
  • Archos at Sep 6, 2012 at 5:20 pm
    But, how is solved in Windows?
    I've tested it using os.Stdout which return the same error type: "The
    handle is invalid.", and with os.Stdin returning: EOF

    Should I fill in an issue?

    El jueves, 6 de septiembre de 2012 14:41:18 UTC+1, minux escribió:

    On Thu, Sep 6, 2012 at 9:37 PM, minux <minu...@gmail.com <javascript:>>wrote:
    Right if you run test binary from a tty. That's because the shell usually
    dup fd 0
    to 1 and 2, so stdout and stderr actually is a tty opened read/write.
    Correction: technically, it's not the shell that duped the fds, it should
    be getty(1)
    or whatever (maybe indirectly) starts your shell (e.g. terminal emulator).
  • Minux at Sep 6, 2012 at 5:31 pm

    On Fri, Sep 7, 2012 at 1:20 AM, Archos wrote:

    I've tested it using os.Stdout which return the same error type: "The
    handle is invalid.", and with os.Stdin returning: EOF
    if reading from os.Stdin returns EOF then i think this is not a bug.
  • Archos at Sep 6, 2012 at 5:39 pm
    Well, the case is that os.Stdin can not be used from "go test" in Windows.

    C:\Documents and Settings\neo\go\src\github.com\arios>go test -v
    === RUN Test
    Write (enter to finish): 2012/09/06 19:34:33 EOF
    exit status 1
    FAIL github.com/arios 0.062s


    El jueves, 6 de septiembre de 2012 18:31:54 UTC+1, minux escribió:

    On Fri, Sep 7, 2012 at 1:20 AM, Archos <raul...@sent.com <javascript:>>wrote:
    I've tested it using os.Stdout which return the same error type: "The
    handle is invalid.", and with os.Stdin returning: EOF
    if reading from os.Stdin returns EOF then i think this is not a bug.
  • Minux at Sep 6, 2012 at 5:43 pm

    On Fri, Sep 7, 2012 at 1:39 AM, Archos wrote:

    Well, the case is that os.Stdin can not be used from "go test" in Windows.

    C:\Documents and Settings\neo\go\src\github.com\arios>go test -v
    === RUN Test
    Write (enter to finish): 2012/09/06 19:34:33 EOF

    exit status 1
    FAIL github.com/arios 0.062s
    you can't use os.Stdin in tests on other platforms either.
    the fact that you can substitute os.Stdout or os.Stderr for os.Stdin on
    Unixes
    is a hack and not portable to windows.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 6, '12 at 1:19p
activeSep 6, '12 at 5:43p
posts10
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase