331

我跑去go get package下载一个包,然后才知道我需要设置我的包,GOPATH否则这个包会污染我的根 Go 安装(我更愿意保持我的 Go 安装干净并将核心与自定义分开)。如何删除以前安装的软件包?

4

6 回答 6

245

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

于 2012-12-09T22:02:30.493 回答
207

您可以删除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

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

于 2015-10-27T15:58:53.187 回答
95

可以按如下方式删除一个 go 包

go get package@none

@none是此处设置为的版本部分none。从而卸下包装。

于 2021-05-20T12:45:20.713 回答
20

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

于 2021-05-09T15:08:01.727 回答
10
#!/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
于 2018-04-27T20:04:08.543 回答
1

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

于 2021-05-13T10:57:02.030 回答