【问题标题】:Removing packages installed with go get删除使用 go get 安装的软件包
【发布时间】:2012-11-27 08:28:23
【问题描述】:

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

【问题讨论】:

标签: go


【解决方案1】:

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

【讨论】:

  • 如果既安全又简单,为什么没有 go 子命令呢?
  • 来自 npm,我们对go 的了解还有很多
  • 在 Mac 上:$GOPATH = $HOME/go
  • 我对 Go 了解得越多,我就越意识到做非常“简单”、“原生”的东西是多么糟糕。我的意思是,你怎么能有安装命令但没有删除命令。怎么样???
  • 是的,真的,依赖关系呢?
【解决方案2】:

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

【讨论】:

  • 如何找到所有项目的依赖关系?
  • 要包含依赖项,请使用-r 标志。但请注意 - 如果某个依赖项被其他包使用,它仍然会被删除。
  • 它应该与 Go 1.16 一起使用吗?对于任何已安装的“最新”(go install foo@latest) 软件包,Go 1.16 都说“不匹配任何软件包”。
【解决方案3】:

一个go包可以如下移除

go get package@none

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

【讨论】:

  • 这是2021年的正确答案。大反响abhisekp!
  • 在 GOPATH 模式下不能使用 path@version 语法
  • 可以确认这适用于 go 版本 go1.16.5(当前日期是 2021 年 8 月)。
  • 这从~/go/bin 中删除了已编译的二进制文件,但包仍保留在~/go/pkg/mod/github.com/... 中。还有一步吗?
  • (osx) 没有为我从~/go/bin 中删除二进制文件。没有错误,命令没有输出。
【解决方案4】:

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

【讨论】:

  • 这可行,但仅用于删除不再从模块中的代码中引用的模块,使用 Go 版本 >= 1.11+。要在 go 模块之外删除使用 go get 或 go install 安装的 go 程序或二进制文件,则必须使用 go clean
【解决方案5】:
#!/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

【讨论】:

  • 谁有这个 zsh 的工作副本?
【解决方案6】:

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

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2010-11-16
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多