我正在运行执行“psql”的代码。它应该返回一些错误代码,因为数据库未启动。
它应该返回
psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
但是它返回标准错误
2013/11/21 15:06:19 exit status 2
exit status 1
这是代码
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("psql").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n",out)
}
最佳答案
因为输出仅从 STDOUT 返回数据,而 psql 错误是在 STDERR 流上打印该数据
您需要阅读 STDERR:
func (c *Cmd) StderrPipe() (io.ReadCloser, error)
关于go - 我如何使用 Go 的 os/exec 库将错误消息输入到我的 Go 程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20130877/