如何列出已安装的Go包?

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

How to list installed go packages

问题

据我所知,go distribution 配备了某种形式的 包管理器。在安装了 go 1.4.1 之后,我运行了 go help 命令,希望找到能够列出本地已安装的 go 包 的子命令,但很遗憾没有找到。

那么该如何做呢?

英文:

To my knowledge go distribution comes with some sort of package manager. After go 1.4.1 installation I've run go help in order to find any sub-command capable of listing locally installed go packages, but unfortunately got none.

So how to do it?

答案1

得分: 100

goinstall现在已经过时了

goinstall已被go get取代。go get用于管理外部/第三方库(例如下载、更新、安装等)。

输入go help get以查看命令行帮助,或查看以下页面:

Command go

关于go命令(博文)

如果您想列出已安装的包,可以使用go list命令:

列出包

要列出工作区中的包,请转到工作区文件夹并运行以下命令:

go list ./...

./表示从当前文件夹开始,...表示递归向下。当然,这适用于任何其他文件夹,而不仅仅是您的go工作区(但通常您感兴趣的是这个)。

列出所有包

在任何文件夹中执行

go list ...

将列出所有包,首先是标准库的包,然后是go工作区中的外部库。

包及其依赖关系

如果您还想查看每个包导入的包,可以尝试使用此自定义格式:

go list -f "{{.ImportPath}} {{.Imports}}" ./...

-f指定列表的替代格式,使用template包的语法。可以通过go help list命令打印可以引用其字段的结构。

如果您想要递归地查看所有依赖关系(递归导入的包的依赖关系),可以使用此自定义格式:

go list -f "{{.ImportPath}} {{.Deps}}" ./...

但通常这是一个很长的列表,您只需要每个包的单个导入("{{.Imports}}")。


还请参阅相关问题:https://stackoverflow.com/questions/55866604/whats-the-go-mod-equivalent-of-npm-outdated/55866702#55866702

英文:

goinstall is now history

goinstall was replaced by go get. go get is used to manage external / 3rd party libraries (e.g. to download them, update them, install them etc).

Type go help get to see command line help, or check out these pages:

Command go

About the go command (blog post)

If you want to list installed packages, you can do that with the go list command:

Listing Packages

To list packages in your workspace, go to your workspace folder and run this command:

go list ./...

./ tells to start from the current folder, ... tells to go down recursively. Of course this works in any other folders not just in your go workspace (but usually that is what you're interested in).

List All Packages

Executing

go list ...

in any folder lists all the packages, including packages of the standard library first followed by external libraries in your go workspace.

Packages and their Dependencies

If you also want to see the imported packages by each package, you can try this custom format:

go list -f "{{.ImportPath}} {{.Imports}}" ./...

-f specifies an alternate format for the list, using the syntax of package template. The struct whose fields can be referenced can be printed by the go help list command.

If you want to see all the dependencies recursively (dependencies of imported packages recursively), you can use this custom format:

go list -f "{{.ImportPath}} {{.Deps}}" ./...

But usually this is a long list and just the single imports ("{{.Imports}}") of each package is what you want.


Also see related question: https://stackoverflow.com/questions/55866604/whats-the-go-mod-equivalent-of-npm-outdated/55866702#55866702

答案2

得分: 29

开始启动Go文档服务器:

godoc --http :6060

访问 http://localhost:6060/pkg

这里将显示所有你的包的列表。

当你安装新的包时,它们不会自动出现。你需要重新启动 godoc

英文:

Start Go documentation server:

godoc --http :6060

Visit http://localhost:6060/pkg

There will be list of all your packages.

When you install new ones they do not appear automatically. You need to restart godoc.

答案3

得分: 2

go list ...非常有用,但对我来说可能有两个问题:

  1. 它会列出所有包,包括标准库包。没有办法只获取显式安装的包(这是我认为更有趣的查询方式)。
  2. 很多时候,我只需要项目中使用的包(即在相应的go.mod文件中列出的包),而不关心其他安装的包(可能只是为了尝试它们而安装的)。go list ...无法解决这个问题。

所以这里有一个稍微不同的方法。假设所有项目都在~/work目录下:

find ~/work -type f -name go.mod \
-exec sed $'/^require ($/,/^)$/!d; /^require ($/d;/^)$/d; /\\/\\/ indirect$/d; s/^\t+//g' {} \; \
| cut -d' ' -f1 \
| sort | uniq

逐行解释:

  1. 查找所有的go.mod文件。
  2. 对每个文件应用sed命令来过滤其内容,具体如下(逐个表达式解释):
    • 提取require( ... )
    • 删除require()行,只保留包名行
    • 删除所有间接包
    • 删除前导制表符<sup>1)</sup>
  3. 提取合格的包名(去除版本信息)
  4. 删除重复的包名

<sup>1)</sup> 注意,sed表达式参数使用bash引用来转义制表符为"\t",以提高可读性而不是使用字面制表符。

英文:

go list ... is quite useful, but there were two possible issues with it for me:

  1. It will list all packages including standard library packages. There is no way to get the explicitly installed packages only (which is what I assume the more interesting inquiry).
  2. A lot of times I need only the packages used in my projects (i.e. those listed in the respective go.mod files), and I don't care about other packages lying around (which may have been installed just to try them out). go list ... doesn't help with that.

So here's a somewhat different take. Assuming all projects are under ~/work:

find ~/work -type f -name go.mod \
-exec sed $&#39;/^require ($/,/^)$/!d; /^require ($/d;/^)$/d; /\\/\\/ indirect$/d; s/^\t+//g&#39; {} \; \
| cut -d&#39; &#39; -f1 \
| sort | uniq

A line by line explanation:

  1. find all go.mod files
  2. apply sed to each file to filter its content as follows (explained expression by expression):
    • extract just the require( ... ) chunks
    • remove the require( and ) lines, so just lines with packages remain
    • remove all indirect packages
    • remove leading tabs <sup>1)</sup>
  3. extract just the qualified package name (drop version information)
  4. remove duplicate package names

<sup>1)</sup> Note the sed expression argument uses bash quoting to escape the TAB character as "\t" for readability over a literal TAB.

答案4

得分: 0

在*nix系统上(可能在使用类似msysgit或cmder的Windows系统上),要查看我安装了哪些软件包,我可以运行以下命令:

history | grep "go get"

但这样的输出很凌乱。出于某种原因,我决定稍微整理一下这个输出,所以我为这个命令创建了一个别名:

history | grep 'go get' | grep -v ' history ' | sed -e $'s/go get /\\\\\ngo get /g' | grep 'go get ' | sed -e $'s/-u //g' | sed -e $'s/-v //g' | sed -e $'s/ &&//g' | grep -v '\\\n' | egrep 'get [a-z]' | sed -e $'s/go get //g' | sed -e $'s/ //g' | sort -u

请不要问我为什么这样做。也许是为了挑战?让我解释一下各个部分的含义:

history:历史记录

grep "go get":在历史记录中使用grep,只显示我们获取了某些内容的行

grep -v " history ":排除我们在历史记录中搜索过"go get"的时间

sed -e $'s/go get /\\\\\ngo get /g':现在,我们在任何"go get "的实例前插入一个新行。现在它们都在开头。

grep "go get ":只过滤以"go get"开头的行

sed -e $'s/-u //g'sed -e $'s/-v //g':删除我们搜索过的标志。你也可以保留它们,但在输出完成时可能会出现重复。

sed -e $'s/ &&//g':有时我们使用多个命令进行安装,使用"&&"连接,所以让我们将它们从行的末尾删除。

grep -v "\\n":我的输出中有其他带有换行符的行,我不需要它们。所以这个命令将它们去掉。

egrep "get [a-z]":确保只获取正确格式的Go软件包URL。

sed -e $'s/go get //g':删除"go get "文本

sed -e $'s/ //g':去掉任何空格(需要过滤掉重复项)

sort -u:现在对剩下的行进行排序并删除重复项。

这在其他系统上完全没有经过测试。再次强调,我相信有更简洁的方法来实现这个目标。只是觉得尝试一下会很有趣。

也许制作一个go ls命令来显示你明确安装的实际软件包会更有趣。但那需要更多的工作。特别是因为我还在学习Go。

输出:

> gols 
code.google.com/p/go.crypto/bcrypt
github.com/golang/lint/golint
github.com/kishorevaishnav/revelgen
github.com/lukehoban/go-find-references
github.com/lukehoban/go-outline
github.com/newhook/go-symbols
github.com/nsf/gocode
github.com/revel/cmd/revel
github.com/revel/revel
github.com/rogpeppe/godef
github.com/tpng/gopkgs
golang.org/x/tools/cmd/goimports
golang.org/x/tools/cmd/gorename
gopkg.in/gorp.v1
sourcegraph.com/sqs/goreturns
英文:

on *nix systems (possibly on windows with bash tools like msysgit or cmder), to see what packages I have installed, I can run:

history | grep &quot;go get&quot;

But thats messy output. For whatever reason I decided to see of i could clean that output up a little so i made an alias for this command:

history | grep &#39;go get&#39; | grep -v &#39; history &#39; | sed -e $&#39;s/go get /\\\\\ngo get /g&#39; | grep &#39;go get &#39; | sed -e $&#39;s/-u //g&#39; | sed -e $&#39;s/-v //g&#39; | sed -e $&#39;s/ &amp;&amp;//g&#39; | grep -v &#39;\\\n&#39; | egrep &#39;get [a-z]&#39; | sed -e $&#39;s/go get //g&#39; | sed -e $&#39;s/ //g&#39; | sort -u

please don't ask why I did this. Challenge maybe? Let me explain the parts

history the history

grep &quot;go get&quot; grep over history and only show lines where we went and got something

grep -v &quot; history &quot; and remove times when we have searched for "got get" in history

sed -e $&#39;s/go get /\\\\\ngo get /g&#39; Now we take any instances of "go get " and shove a new line in front of it. Now they're all at the beginning.

grep &quot;go get &quot; filter only lines that now start with "go get"

sed -e $&#39;s/-u //g&#39; and sed -e $&#39;s/-v //g&#39; remove flags we have searched for. You could possibly leave them in but may get duplicates when output is done.

sed -e $&#39;s/ &amp;&amp;//g&#39; some times we install with multiple commands using '&&' so lets remove them from the ends of the line.

grep -v &quot;\\\n&quot; my output had other lines with newlines printed I didnt need. So this got rid of them

egrep &quot;get [a-z]&quot; make sure to get properly formatted go package urls only.

sed -e $&#39;s/go get //g&#39; remove the "go get " text

sed -e $&#39;s/ //g&#39; strip any whitespace (needed to filter out duplicates)

sort -u now sort the remaining lines and remove duplicates.

This is totally untested on other systems. Again, I am quite sure there is a cleaner way to do this. Just thought it would be fun to try.

It would also probably be more fun to make a go ls command to show the actual packages you explicitly installed. But thats a lot more work. Especially since i'm only still learning Go.

Output:

&gt; gols 
code.google.com/p/go.crypto/bcrypt
github.com/golang/lint/golint
github.com/kishorevaishnav/revelgen
github.com/lukehoban/go-find-references
github.com/lukehoban/go-outline
github.com/newhook/go-symbols
github.com/nsf/gocode
github.com/revel/cmd/revel
github.com/revel/revel
github.com/rogpeppe/godef
github.com/tpng/gopkgs
golang.org/x/tools/cmd/goimports
golang.org/x/tools/cmd/gorename
gopkg.in/gorp.v1
sourcegraph.com/sqs/goreturns

huangapple
  • 本文由 发表于 2015年1月27日 16:40:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/28166249.html
匿名

发表评论

匿名网友

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

确定