您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Go获取Python版本

5b51 2022/1/14 8:22:14 python 字数 1724 阅读 518 来源 www.jb51.cc/python

我正在尝试使用Go获取我的 Python版本: import ( "log" "os/exec" "strings" ) func verifyPythonVersion() { _, err := exec.LookPath("python") if err != nil { log.Fatalf("No python version l

概述

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版本所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶