检查Go中是否安装了软件包
我需要检查是否安装了一些软件包,但我需要使用代码,而不是shell中的检查Go中是否安装了软件包,go,Go,我需要检查是否安装了一些软件包,但我需要使用代码,而不是shell中的go list工具。我找到了一个解决方案,但速度非常慢(2-3秒)。这是我当前的代码: out, err := exec.Command("sh", "-c", "go list all").Output() if err != nil { output := strings.Split(string(out), "\n") for _, value := range output { if v
go list
工具。我找到了一个解决方案,但速度非常慢(2-3秒)。这是我当前的代码:
out, err := exec.Command("sh", "-c", "go list all").Output()
if err != nil {
output := strings.Split(string(out), "\n")
for _, value := range output {
if value == "github.com/some/package" {
// package is installed
}
}
}
所以基本上你回答了你自己的问题。你想要更快的解决方案吗?尝试“调整”
go list
命令
要检查是否存在单个包,您可以将该单个包传递到go list
,如果存在,它将输出该包,否则输出将是一条错误消息
例如,执行
go list github.com/some/package
如果存在github.com/some/package
,则输出将为:
github.com/some/package
github.com/some/package
github.com/other/package
您还可以将多个软件包传递给go list
:
go list github.com/some/package github.com/other/package
输出将是:
github.com/some/package
github.com/some/package
github.com/other/package
如果传递的包不存在,则输出如下:
can't load package: package github.com/some/package: cannot find package "github.com/some/package" in any of:
/usr/local/go/src/github.com/some/package (from $GOROOT)
<GOPATH-here>/src/github.com/some/package (from $GOPATH)
如果您希望其中包含一些包,请附加…
:
go list github.com/some/package/...
有关更多选项和可能性,请运行“转到帮助列表”,并查看相关问题:非常感谢,现在速度快了1000倍