The command "psql" should throw an error, and I am trying to read stderr and print it in the Go program. I use ioutil.ReadAll to read the data from stderr, and stdout.
Unfortunately it is not reading from stderr at all. ioutil.ReadAll returns an error, which is not the error I am expecting.
The error I get is
read |0: bad file descriptor
Here is the code.
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)
}
d := cmd.Wait()
if d != nil {
fmt.Println(d)
}
stdo,g := ioutil.ReadAll(stdout)
stde,f := ioutil.ReadAll(stderr)
if g != nil {
fmt.Println(g)
}
if f !=nil {
fmt.Println(f)
}
fmt.Printf("Standard err is %s
", stde)
fmt.Printf("Standard out is %s
",stdo)
}