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?

CombinedOutput Example 
<https://pkg.go.dev/os/exec#example-Cmd.CombinedOutput>
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

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/581e1d50-7c5a-4895-8589-350923ff8477n%40googlegroups.com.

Reply via email to