删除使用go get安装的软件包


227

我跑go get package了一个包,然后下载了一个包,然后才知道需要设置我的GOPATH包,否则该包会弄脏我的Go根安装目录(我更希望保持Go的安装目录干净,并使核心与自定义分开)。如何删除以前安装的软件包?


Answers:


187

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


5
最初,我寻找了不存在的$ GOPATH / pkg / architecture /。然后我意识到您所指的是$ GOPATH / pkg / {{architecture}},例如$ GOPATH / pkg / windows_amd64。
Nucleon

1
默认值GOPATH/usr/lib/go
Flimm

245
如果安全且简单,为什么没有执行该命令的go子命令?
Bengt 2014年

71
来自npm的消息,我们还有很多工作go
要做

4
在Mac上:$ GOPATH = $ HOME / go
Ricardo Martins

151

您可以使用删除go install(或go get)为软件包生成的存档文件和可执行二进制文件go clean -i importpath...。这些通常分别位于$GOPATH/pkg和下$GOPATH/bin

确保包括...在importpath上,因为看起来,如果程序包包含可执行文件,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版中的工具。


2
您如何找到所有项目依赖项?
Michael Mallett '18

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.