Removing packages installed with go get

前端 未结 4 1972
[愿得一人]
[愿得一人] 2020-12-22 14:48

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 woul

4条回答
  •  北海茫月
    2020-12-22 15:50

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

提交回复
热议问题