英文:
Removing packages installed with go get
问题
我之前运行了go get package
来下载一个包,后来才发现我需要设置我的GOPATH
,否则这个包会污染我的根目录Go安装(我更喜欢保持我的Go安装干净,并将核心和自定义分开)。我如何删除之前安装的包?
英文:
I ran go get package
to download a package before learning that I needed to set my GOPATH
otherwise that package sullies my root Go install (I would much prefer to keep my Go install clean and separate core from custom). How do I remove packages installed previously?
答案1
得分: 271
只需安全地删除源目录和编译的软件包文件即可。在$GOPATH/src
下找到源目录,在$GOPATH/pkg/<architecture>
下找到软件包文件,例如:$GOPATH/pkg/windows_amd64
。
英文:
It's safe to just delete the source directory and compiled package file. Find the source directory under $GOPATH/src
and the package file under $GOPATH/pkg/<architecture>
, for example: $GOPATH/pkg/windows_amd64
.
答案2
得分: 255
一个Go包可以按照以下方式被移除。
go get package@none
这里的@none
是版本部分设置为none
。因此移除了该包。
英文:
A go package can be removed as follows.
go get package@none
Here @none
is the version part set as none
. Thus removing the package.
答案3
得分: 227
您可以使用go clean -i importpath...
命令删除go install
(或go get
)生成的存档文件和可执行二进制文件。这些文件通常位于$GOPATH/pkg
和$GOPATH/bin
目录下。
请确保在导入路径上包含...
,因为如果一个包包含可执行文件,go clean -i
只会删除该文件,而不会删除子包的存档文件,例如下面示例中的gore/gocode
。
然后,需要手动从$GOPATH/src
目录中删除源代码。
go clean
命令有一个-n
标志,用于进行干运行,打印将要执行的操作而不实际执行,这样您可以确保操作的准确性(请参阅go help clean
)。它还有一个诱人的-r
标志,用于递归清理依赖项,但您可能不想实际使用它,因为通过干运行您会发现它会删除大量的标准库存档文件!
以下是一个完整的示例,您可以基于此示例编写脚本:
$ go get -u github.com/motemen/gore
$ which gore
/Users/ches/src/go/bin/gore
$ go clean -i -n github.com/motemen/gore...
cd /Users/ches/src/go/src/github.com/motemen/gore
rm -f gore gore.exe gore.test gore.test.exe commands commands.exe commands_test commands_test.exe complete complete.exe complete_test complete_test.exe debug debug.exe helpers_test helpers_test.exe liner liner.exe log log.exe main main.exe node node.exe node_test node_test.exe quickfix quickfix.exe session_test session_test.exe terminal_unix terminal_unix.exe terminal_windows terminal_windows.exe utils utils.exe
rm -f /Users/ches/src/go/bin/gore
cd /Users/ches/src/go/src/github.com/motemen/gore/gocode
rm -f gocode.test gocode.test.exe
rm -f /Users/ches/src/go/pkg/darwin_amd64/github.com/motemen/gore/gocode.a
$ go clean -i github.com/motemen/gore...
$ which gore
$ tree $GOPATH/pkg/darwin_amd64/github.com/motemen/gore
/Users/ches/src/go/pkg/darwin_amd64/github.com/motemen/gore
0 directories, 0 files
# 如果那个空目录真的让您不爽...
$ rmdir $GOPATH/pkg/darwin_amd64/github.com/motemen/gore
$ rm -rf $GOPATH/src/github.com/motemen/gore
请注意,这些信息基于Go版本1.5.1中的go
工具。
英文:
You can delete the archive files and executable binaries that go install
(or go get
) produces for a package with go clean -i importpath...
. These normally reside under $GOPATH/pkg
and $GOPATH/bin
, respectively.
Be sure to include ...
on the importpath, since it appears that, if a package includes an executable, go clean -i
will only remove that and not archive files for subpackages, like gore/gocode
in the example below.
Source code then needs to be removed manually from $GOPATH/src
.
go clean
has an -n
flag for a dry run that prints what will be run without executing it, so you can be certain (see go help clean
). It also has a tempting -r
flag to recursively clean dependencies, which you probably don't want to actually use since you'll see from a dry run that it will delete lots of standard library archive files!
A complete example, which you could base a script on if you like:
$ go get -u github.com/motemen/gore
$ which gore
/Users/ches/src/go/bin/gore
$ go clean -i -n github.com/motemen/gore...
cd /Users/ches/src/go/src/github.com/motemen/gore
rm -f gore gore.exe gore.test gore.test.exe commands commands.exe commands_test commands_test.exe complete complete.exe complete_test complete_test.exe debug debug.exe helpers_test helpers_test.exe liner liner.exe log log.exe main main.exe node node.exe node_test node_test.exe quickfix quickfix.exe session_test session_test.exe terminal_unix terminal_unix.exe terminal_windows terminal_windows.exe utils utils.exe
rm -f /Users/ches/src/go/bin/gore
cd /Users/ches/src/go/src/github.com/motemen/gore/gocode
rm -f gocode.test gocode.test.exe
rm -f /Users/ches/src/go/pkg/darwin_amd64/github.com/motemen/gore/gocode.a
$ go clean -i github.com/motemen/gore...
$ which gore
$ tree $GOPATH/pkg/darwin_amd64/github.com/motemen/gore
/Users/ches/src/go/pkg/darwin_amd64/github.com/motemen/gore
0 directories, 0 files
# If that empty directory really bugs you...
$ rmdir $GOPATH/pkg/darwin_amd64/github.com/motemen/gore
$ rm -rf $GOPATH/src/github.com/motemen/gore
Note that this information is based on the go
tool in Go version 1.5.1.
答案4
得分: 49
你可以使用go mod tidy
来清理未使用的包。
英文:
You can use go mod tidy
to clean unused packages
答案5
得分: 12
goclean() {
local pkg=$1; shift || return 1
local ost
local cnt
local scr
清除包源目录中的对象文件(忽略错误)
go clean -i $pkg &>/dev/null
设置本地变量
[[ "$(uname -m)" == "x86_64" ]]
&& ost="$(uname)";ost="${ost,,}_amd64"
&& cnt="${pkg//[^/]}"
删除源目录和编译后的包目录
if (("${#cnt}" == "2")); then
rm -rf "${GOPATH%%:}/src/${pkg%/}"
rm -rf "${GOPATH%%:}/pkg/${ost}/${pkg%/}"
elif (("${#cnt}" > "2")); then
rm -rf "${GOPATH%%:}/src/${pkg%//}"
rm -rf "${GOPATH%%:}/pkg/${ost}/${pkg%//}"
fi
重新加载当前shell
source ~/.bashrc
}
英文:
#!/bin/bash
goclean() {
local pkg=$1; shift || return 1
local ost
local cnt
local scr
# Clean removes object files from package source directories (ignore error)
go clean -i $pkg &>/dev/null
# Set local variables
[[ "$(uname -m)" == "x86_64" ]] \
&& ost="$(uname)";ost="${ost,,}_amd64" \
&& cnt="${pkg//[^\/]}"
# Delete the source directory and compiled package directory(ies)
if (("${#cnt}" == "2")); then
rm -rf "${GOPATH%%:*}/src/${pkg%/*}"
rm -rf "${GOPATH%%:*}/pkg/${ost}/${pkg%/*}"
elif (("${#cnt}" > "2")); then
rm -rf "${GOPATH%%:*}/src/${pkg%/*/*}"
rm -rf "${GOPATH%%:*}/pkg/${ost}/${pkg%/*/*}"
fi
# Reload the current shell
source ~/.bashrc
}
Usage:
# Either launch a new terminal and copy `goclean` into the current shell process,
# or create a shell script and add it to the PATH to enable command invocation with bash.
goclean github.com/your-username/your-repository
答案6
得分: 1
人,我昨天遇到了同样的问题。在$GOPATH/pkg/<architecture>
中找不到任何东西。然后,我意识到在我的$HOME中有一个go目录。所以,我转到$HOME/<username>/go/pkg/mod/github.com
,看到了我通过go get
从github安装的所有包。
英文:
Man, i got same problem yesterday. Could't find anything in $GOPATH/pkg/<architecture>
. Then, i realized that there was a go directory in my $HOME. So, i moved to $HOME/<username>/go/pkg/mod/github.com
and saw all package i had installed from github by go get
答案7
得分: 1
我在我的主目录中删除了整个go文件夹,然后使用go mod tidy
重新下载了我项目实际使用的所有依赖(除了实际版本之外,我还有一堆旧版本)。
我恢复了大约3GB的磁盘空间。
英文:
I deleted the whole go folder in my home directory and then with go mod tidy
I redownloaded all dependencies actually used by my project (I had a bunch of old versions in addition to the actual versions used).
I recovered about 3GB of disk space.
答案8
得分: -4
要删除对一个模块的依赖并降级需要它的模块:
go get example.com/mod@none
来源:运行 go help get
。
英文:
To remove a dependency on a module and downgrade modules that require it:
go get example.com/mod@none
source: run go help get
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论