英文:
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
以查看命令行帮助,或查看以下页面:
关于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:
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 ...
非常有用,但对我来说可能有两个问题:
- 它会列出所有包,包括标准库包。没有办法只获取显式安装的包(这是我认为更有趣的查询方式)。
- 很多时候,我只需要项目中使用的包(即在相应的
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
逐行解释:
- 查找所有的
go.mod
文件。 - 对每个文件应用
sed
命令来过滤其内容,具体如下(逐个表达式解释):- 提取
require(
...)
块 - 删除
require(
和)
行,只保留包名行 - 删除所有间接包
- 删除前导制表符<sup>1)</sup>
- 提取
- 提取合格的包名(去除版本信息)
- 删除重复的包名
<sup>1)</sup> 注意,sed表达式参数使用bash引用来转义制表符为"\t",以提高可读性而不是使用字面制表符。
英文:
go list ...
is quite useful, but there were two possible issues with it for me:
- 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).
- 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 $'/^require ($/,/^)$/!d; /^require ($/d;/^)$/d; /\\/\\/ indirect$/d; s/^\t+//g' {} \; \
| cut -d' ' -f1 \
| sort | uniq
A line by line explanation:
- find all
go.mod
files - 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>
- extract just the
- extract just the qualified package name (drop version information)
- 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 "go get"
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 '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
please don't ask why I did this. Challenge maybe? Let me explain the parts
history
the history
grep "go get"
grep over history and only show lines where we went and got something
grep -v " history "
and remove times when we have searched for "got get" in history
sed -e $'s/go get /\\\\\ngo get /g'
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 "go get "
filter only lines that now start with "go get"
sed -e $'s/-u //g'
and sed -e $'s/-v //g'
remove flags we have searched for. You could possibly leave them in but may get duplicates when output is done.
sed -e $'s/ &&//g'
some times we install with multiple commands using '&&' so lets remove them from the ends of the line.
grep -v "\\\n"
my output had other lines with newlines printed I didnt need. So this got rid of them
egrep "get [a-z]"
make sure to get properly formatted go package urls only.
sed -e $'s/go get //g'
remove the "go get " text
sed -e $'s/ //g'
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:
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论