使用Go获取Python版本
我正在尝试使用Go获取我的Python版本:使用Go获取Python版本,python,go,Python,Go,我正在尝试使用Go获取我的Python版本: import ( "log" "os/exec" "strings" ) func verifyPythonVersion() { _, err := exec.LookPath("python") if err != nil { log.Fatalf("No python version located") } out, err := exec.Command("pytho
import (
"log"
"os/exec"
"strings"
)
func verifyPythonVersion() {
_, err := exec.LookPath("python")
if err != nil {
log.Fatalf("No python version located")
}
out, err := exec.Command("python", "--version").Output()
log.Print(out)
if err != nil {
log.Fatalf("Error checking Python version with the 'python' command: %v", err)
}
fields := strings.Fields(string(out))
log.Print(fields)
}
func main() {
verifyPythonVersion()
}
这将返回空切片:
2014/01/03 20:39:53 []
2014/01/03 20:39:53 []
知道我做错了什么吗
$ python --version
Python 2.7.2
$ python --version 1>/dev/null # hide stdout
Python 2.7.2
$ python --version 2>/dev/null # hide stderr
我们可以得出这样的结论:输出到stderr。现在我看了一下Go的文档,猜猜看,cmd.Output
只捕获stdout()。您应该使用cmd.CombinedOutput
():
CombinedOutput运行该命令并返回其组合的标准输出和标准错误