Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Go获取Python版本_Python_Go - Fatal编程技术网

使用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

我正在尝试使用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("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运行该命令并返回其组合的标准输出和标准错误