如何使用Go'将错误消息输入Go程序;s os/exec库?
我正在运行执行“psql”的代码。它应该返回一些错误代码,因为数据库未启动 它应该会回来如何使用Go'将错误消息输入Go程序;s os/exec库?,go,Go,我正在运行执行“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 sta
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流上打印该数据 您需要阅读标准:
func (c *Cmd) StderrPipe() (io.ReadCloser, error)
您可能希望使用Postgres客户端库,比如,而不是运行psql。关键是我希望能够打印出标准。有意义!只是觉得指向pq的指针可能会有帮助。我想你是指STDOUT而不是STDIN@tomwilde是的。修正,谢谢:)