概述
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(docs).你应该使用cmd.CombinedOutput(docs):
CombinedOutput runs the command and returns its combined standard output and standard error.
总结
以上是编程之家为你收集整理的使用Go获取Python版本全部内容,希望文章能够帮你解决使用Go获取Python版本所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧