如何列出已安装在$GOBIN中的软件包?

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

How do I list packages whose binaries are installed in $GOBIN?

问题

如果我理解正确,go install 命令会将二进制文件放在 $GOBIN 目录中。

我经常会忘记哪些包提供了 $GOBIN 中的哪些二进制文件,以及我安装了哪些包。

有没有一种方法可以列出当前已经通过 go install 安装的包呢?

显然,我可以执行 ls $GOBIN 命令来获取二进制文件的名称。然而,我正在寻找一种获取包及其完整名称(例如 github.com/operator-framework/operator-sdk)的方法。

更新: 根据这里的建议,go list ... 命令似乎不适用于我的情况,因为该命令要求我在包含 go.mod 文件的目录中。我理解 go list 命令是用于列出一个项目的包。在我的情况下,我需要找出所有安装到我系统/用户的 $GOBIN 中的包。

英文:

If I understand correctly, go install puts binaries in $GOBIN directory.

I tend to forget which packages provide which binaries in $GOBIN and which packages I go install-ed.

Is there a way to list packages that are currently go install-ed?

I could, obviously, do ls $GOBIN and that'll give me names of binaries. However, I'm looking for a way to get a list of packages with their long names, e.g. github.com/operator-framework/operator-sdk.

Update: go list ... (suggested here) does not seem to apply to my case because this command expects me to be in a directory containing go.mod file. I understand that go list is designated for listing packages of a project. In my case, I need to find out all packages that are installed into $GOBIN of my system/user.

答案1

得分: 6

对于$GOBIN中的每个二进制文件,您可以使用go version -m命令显示有关Go版本和用于构建该二进制文件的模块版本的信息。例如,这是我对~/go/bin/goimports运行的go version -m命令的结果。

$ go version -m ~/go/bin/goimports
/Users/jayconrod/go/bin/goimports: go1.15.2
	path	golang.org/x/tools/cmd/goimports
	mod	golang.org/x/tools	v0.0.0-20200910165216-53e29e9d1252	h1:qn28WK3EvdJOSlyZFNeWnaEX8X5GiQv+8mrw9mYrXko=
	dep	golang.org/x/mod	v0.3.0	h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
	dep	golang.org/x/xerrors	v0.0.0-20200804184101-5ec99f83aff1	h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=

path行显示了安装的main包的名称。看起来我需要更新它。

$ go install golang.org/x/tools/cmd/goimports@latest
$ go version -m ~/go/bin/goimports
/Users/jayconrod/go/bin/goimports: go1.17rc1
	path	golang.org/x/tools/cmd/goimports
	mod	golang.org/x/tools	v0.1.5	h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
	dep	golang.org/x/mod	v0.4.2	h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
	dep	golang.org/x/sys	v0.0.0-20210510120138-977fb7262007	h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
	dep	golang.org/x/xerrors	v0.0.0-20200804184101-5ec99f83aff1	h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
英文:

For each binary in $GOBIN, you can use go version -m to show information about the version of Go and the module versions that were used to build that binary. For example, here's go version -m ~/go/bin/goimports for me.

$ go version -m ~/go/bin/goimports
/Users/jayconrod/go/bin/goimports: go1.15.2
	path	golang.org/x/tools/cmd/goimports
	mod	golang.org/x/tools	v0.0.0-20200910165216-53e29e9d1252	h1:qn28WK3EvdJOSlyZFNeWnaEX8X5GiQv+8mrw9mYrXko=
	dep	golang.org/x/mod	v0.3.0	h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
	dep	golang.org/x/xerrors	v0.0.0-20200804184101-5ec99f83aff1	h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=

The path line shows the name of the main package that was installed. Looks like I need to update it.

$ go install golang.org/x/tools/cmd/goimports@latest
$ go version -m ~/go/bin/goimports
/Users/jayconrod/go/bin/goimports: go1.17rc1
	path	golang.org/x/tools/cmd/goimports
	mod	golang.org/x/tools	v0.1.5	h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
	dep	golang.org/x/mod	v0.4.2	h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
	dep	golang.org/x/sys	v0.0.0-20210510120138-977fb7262007	h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
	dep	golang.org/x/xerrors	v0.0.0-20200804184101-5ec99f83aff1	h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=

huangapple
  • 本文由 发表于 2021年7月16日 21:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/68409450.html
匿名

发表评论

匿名网友

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

确定