I found that through an experiment that I get an error due to the fact that I'm calling
stdo,g := ioutil.ReadAll(stdout) stde,f := ioutil.ReadAll(stderr)
after
d := cmd.Wait()
so what happens when stdout, stderr pipe closes after cmd.Wait() returns.
Here are the code comments for cmd.StderrPipe()
Thus, it is obvious that we cannot read stdout and stderr after they are closed.
We cannot read them before running the command. Therefore, we must put them between start and expectation.
Here is the code that fixes this.
package main import ( "fmt" "os/exec" "io/ioutil" ) func main() { cmd := exec.Command("psql") stdout, err := cmd.StdoutPipe() if err != nil { fmt.Printf("Error: %s", err) } stderr, err := cmd.StderrPipe() if err != nil { fmt.Printf("Error: %s", err) } err = cmd.Start() if err != nil { fmt.Printf("Start error %s",err) } stdo,g := ioutil.ReadAll(stdout) stde,f := ioutil.ReadAll(stderr) d := cmd.Wait() if d != nil { fmt.Println(d) } if g != nil { fmt.Println(g) } if f !=nil { fmt.Println(f) } fmt.Printf("Standard err is %s \n", stde) fmt.Printf("Standard out is %s \n",stdo) }
ppone
source share