删除随go get安装的软件包

时间:2012-12-09 21:54:06

标签: go

我跑了go get package下载了一个软件包,然后才知道我需要设置GOPATH,否则该软件包会玷污我的root Go安装程序(我更希望保持我的Go安装清洁并将内核与自定义)。如何删除先前安装的软件包?

8 个答案:

答案 0 :(得分:143)

删除源目录和已编译的包文件是安全的。找到$GOPATH/src下的源目录和$GOPATH/pkg/<architecture>下的包文件,例如:$GOPATH/pkg/windows_amd64

答案 1 :(得分:123)

您可以删除go install(或go get)为包含go clean -i importpath...的包生成的存档文件和可执行二进制文件。这些通常分别位于$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

# If that empty directory really bugs you...
$ rmdir $GOPATH/pkg/darwin_amd64/github.com/motemen/gore

$ rm -rf $GOPATH/src/github.com/motemen/gore

请注意,此信息基于Go 1.5.1版中的go工具。

答案 2 :(得分:7)

一个go包可以如下删除

go get package@none

这里@none是版本部分,这里设置为none。从而删除包。

答案 3 :(得分:6)

现在是2020,现在有一个命令:

go clean -modcache

答案 4 :(得分:3)

#!/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
}

用法:

# 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

答案 5 :(得分:2)

您可以使用 go mod tidy 清理未使用的包

答案 6 :(得分:0)

对于使用Go 1.11模块的用户,只需运行go mod tidy。来自the golang wiki

  

go mod tidy —从go.mod中修剪所有不再需要的依赖项,并添加OS,架构和构建标记的其他组合所需的任何依赖项

答案 7 :(得分:0)

伙计,我昨天遇到了同样的问题。在 $GOPATH/pkg/<architecture> 中找不到任何内容。然后,我意识到我的 $HOME 中有一个 go 目录。所以,我搬到了 $HOME/<username>/go/pkg/mod/github.com 并看到了我从 github 安装的所有软件包 go get

相关问题