英文:
Check for installed packages in 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 value == "github.com/some/package" {
// 软件包已安装
}
}
}
英文:
I need to check if some packages are installed but I need to do that using code, not go list
tool in shell. I found a solution but it's very slow (2-3 seconds). This is my current code:
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
}
}
}
答案1
得分: 8
所以基本上你自己回答了你的问题。你想要一个更快的解决方案?尝试“调整”go list
命令。
要检查单个包是否存在,你可以将该单个包传递给go list
,如果存在,它将输出该包,否则输出将是一个错误消息。
例如,执行以下命令:
go list github.com/some/package
如果github.com/some/package
存在,输出将是:
github.com/some/package
你也可以将多个包传递给go list
:
go list github.com/some/package github.com/other/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
文件,你会得到不同的消息:
can't load package: package github.com/some/package: no buildable Go source files in <GOPATH-here>/src/github.com/some/package
如果你期望其中包含一些包,可以在末尾添加...
:
go list github.com/some/package/...
要了解更多选项和可能性,请运行go help list
,并参考相关问题:https://stackoverflow.com/questions/28166249/how-to-list-installed-go-packages/28166550#28166550
英文:
So basically you answered your own question. You want a faster solution? Try "tuning" the go list
command.
To check the existence of a single package, you may pass that single package to go list
, and if it exists, it will output it, else the output will be an error message.
For example, executing
go list github.com/some/package
If github.com/some/package
exists, output will be:
github.com/some/package
You can also pass multiple packages to go list
:
go list github.com/some/package github.com/other/package
And output will be:
github.com/some/package
github.com/other/package
If the passed package doesn't exist, output will be something like:
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)
Also note that if the package you pass does not contain *.go
files, you get a different message:
can't load package: package github.com/some/package: no buildable Go source files in <GOPATH-here>/src/github.com/some/package
If you expect some package inside it, append the ...
:
go list github.com/some/package/...
For more options and possibilities, run go help list
, and see related question: https://stackoverflow.com/questions/28166249/how-to-list-installed-go-packages/28166550#28166550
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论