First time contribution, unsure whether I need to open an issue or direct CL

122 views
Skip to first unread message

Ramil M

unread,
Apr 3, 2022, 11:49:30 AM4/3/22
to golang-nuts
Hi everyone,

I want to make some example improvement suggestion in os/exec package. However I am not sure if I need to submit code change through Gerrit or post  a new issue on GitHub first? Could you please advise?

func ExampleCmd_CombinedOutput() {
cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
}
CombinedOutput example listed above will always print only stdout, but not combined stdout and stderr as intended to be shown in the example.

I propose to make a change as:

func ExampleCmd_CombinedOutput() {
    cmd := exec.Command("sh", "-c", "echo stdout; cat badfile")
    stdoutStderr, err := cmd.CombinedOutput()
    if err != nil {
        fmt.Println("some error occured:")
        fmt.Printf("%s\n", stdoutStderr)
    } else {
        fmt.Println("function executed successfully")
    }
}

In the snippet above, the error will occur when shell will try to cat non-existent file, 
hence we will have the execution with both stdout and stderr.

Thank you.
Ramil

Ian Lance Taylor

unread,
Apr 3, 2022, 12:15:14 PM4/3/22
to Ramil M, golang-nuts
On Sun, Apr 3, 2022 at 8:49 AM Ramil M <mirha...@gmail.com> wrote:
>
> I want to make some example improvement suggestion in os/exec package. However I am not sure if I need to submit code change through Gerrit or post a new issue on GitHub first? Could you please advise?

It is not necessary to open an issue if you intend to submit a change.
You can simply submit a change through Gerrit as described at
https://go.dev/doc/contribute. Thanks.


> CombinedOutput Example
> func ExampleCmd_CombinedOutput() {
> cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
> stdoutStderr, err := cmd.CombinedOutput()
> if err != nil {
> log.Fatal(err)
> }
> fmt.Printf("%s\n", stdoutStderr)
> }
> CombinedOutput example listed above will always print only stdout, but not combined stdout and stderr as intended to be shown in the example.
>
> I propose to make a change as:
>
> func ExampleCmd_CombinedOutput() {
> cmd := exec.Command("sh", "-c", "echo stdout; cat badfile")
> stdoutStderr, err := cmd.CombinedOutput()
> if err != nil {
> fmt.Println("some error occured:")
> fmt.Printf("%s\n", stdoutStderr)
> } else {
> fmt.Println("function executed successfully")
> }
> }
>
> In the snippet above, the error will occur when shell will try to cat non-existent file,
> hence we will have the execution with both stdout and stderr.

When I run the original example on a Linux system, I see

stdout
stderr

So I'm not sure why anything needs to be changed here.

Ian

Brian Candler

unread,
Apr 3, 2022, 3:15:17 PM4/3/22
to golang-nuts
On Sunday, 3 April 2022 at 16:49:30 UTC+1 mirha...@gmail.com wrote:
CombinedOutput example listed above will always print only stdout, but not combined stdout and stderr as intended to be shown in the example.

Works for me (macOS 10.12, go 1.18)

If this doesn't work for you, it could be that the 'sh' on your system isn't a POSIX-compatible shell.  What happens if you change 'sh' to 'bash'?
 
If it still doesn't work, can you say some more about the system you're running on? Is it anything Windows-based (e.g. WSL or Cygwin?)

Ramil M

unread,
Apr 3, 2022, 6:06:49 PM4/3/22
to golang-nuts
I am sorry, I may have not made myself clear earlier. This code is valid and actually works fine for me. 
I was just thinking that this particular example is very specific, where you manipulate with bash, so that it forwards stdout to stderr in it's second command(echo 1>&2 stderr).
So in this case we actually are not getting an *exec.ExitError when we run the code, but forwarded stderr output by shell instead.
If for instance we run any other command in bash, that will fail and output to stderr, the original example will break and exit with log.Fatal(err).
So I was wondering if  rewiring the example will be accepted by the go community.

Thank you all for your inputs. 

Ian Lance Taylor

unread,
Apr 3, 2022, 11:33:59 PM4/3/22
to Ramil M, golang-nuts
On Sun, Apr 3, 2022 at 3:07 PM Ramil M <mirha...@gmail.com> wrote:
>
> I am sorry, I may have not made myself clear earlier. This code is valid and actually works fine for me.
> I was just thinking that this particular example is very specific, where you manipulate with bash, so that it forwards stdout to stderr in it's second command(echo 1>&2 stderr).
> So in this case we actually are not getting an *exec.ExitError when we run the code, but forwarded stderr output by shell instead.
> If for instance we run any other command in bash, that will fail and output to stderr, the original example will break and exit with log.Fatal(err).
> So I was wondering if rewiring the example will be accepted by the go community.

Sorry for misunderstanding.

The idea of the example is that we write some text to stdout, and
write some text to stderr, and the CombinedOutput method returns both.
It's just intended to show what the CombinedOutput method does. I'm
not sure your suggested change makes that clearer.

Ian



> On Sunday, April 3, 2022 at 10:15:17 PM UTC+3 Brian Candler wrote:
>>
>> On Sunday, 3 April 2022 at 16:49:30 UTC+1 mirha...@gmail.com wrote:
>>>
>>> CombinedOutput example listed above will always print only stdout, but not combined stdout and stderr as intended to be shown in the example.
>>
>>
>> Works for me (macOS 10.12, go 1.18)
>>
>> If this doesn't work for you, it could be that the 'sh' on your system isn't a POSIX-compatible shell. What happens if you change 'sh' to 'bash'?
>>
>> If it still doesn't work, can you say some more about the system you're running on? Is it anything Windows-based (e.g. WSL or Cygwin?)
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/fc3123b6-da58-41c3-a640-76e69c5cd659n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages