检查Go中已安装的软件包。

huangapple go评论65阅读模式
英文:

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&#39;t load package: package github.com/some/package: cannot find package &quot;github.com/some/package&quot; in any of:
	/usr/local/go/src/github.com/some/package (from $GOROOT)
	&lt;GOPATH-here&gt;/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&#39;t load package: package github.com/some/package: no buildable Go source files in &lt;GOPATH-here&gt;/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

huangapple
  • 本文由 发表于 2017年1月27日 15:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/41888844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定