4

when I get golang.org/x/tools/cmd/goimports

go get golang.org/x/tools/cmd/goimports

...I get the following error:

package golang.org/x/tools/cmd/goimports: unrecognized import path "golang.org/x/tools/cmd/goimports"

and I tried to compile the goimports from source code, so I download it from

http://github.com/golang/tools.git and https://github.com/bradfitz/goimports

and how to compile it ?

JohnsonDiao
  • 167
  • 1
  • 1
  • 7
  • 1
    `go get golang.org/x/tools/cmd/goimports` works fine for me. What version of Go do you have? And what is your Go environment like? (`go version` and `go env` output). What does `go get -v -u golang.org/x/tools/cmd/goimports` say? If you're going to get source manually you should use git/hg commands to fetch the repository and make sure it ends up in the correct place under `GOPATH` (e.g. under `$GOPATH/src/golang.org/x/tools`). As for how to build, see [How to Write Go Code](https://golang.org/doc/code.html); but you really should fix your `go get` problem. – Dave C Aug 07 '15 at 17:03
  • You're using Linux, right? Which distro? If you're using Ubuntu or any of the other Debian variants, you could try this: http://stackoverflow.com/a/23832292/4257217 If you do, make sure you add `/usr/local/go` and `/usr/bin/go` to the `sudo rm -rf` command. – GreenRaccoon23 Aug 08 '15 at 05:58
  • If that doesn't help, I'm guessing that your $GOROOT is set incorrectly. What does `echo $GOROOT` say? If it says nothing, check whether `/usr/local/go` exists. – GreenRaccoon23 Aug 08 '15 at 06:02
  • 1
    @Dave C Sorry for the late reply. I can't fix the problem of "go get", because the golang.org/x/tools is blocked by the gov. I tried to run "go build" under each of the folder golang/tools/ and then, run "go build" under goimports/ , it worked. I got goimport file. I don't know why. Maybe goimports depend on some packets which need to be build bofore. I am confused how the golang.org/x/tools is organized. If I want to build all of the tools, how to set GOPATH, and where to run "go build" – JohnsonDiao Aug 11 '15 at 08:53

2 Answers2

14

This answer will give an option to work around a failed go get due to network blocking. Note that if there is a configuration problem with your Go installation (such as an incorrect GOPATH), such failures should not be worked around but the underlying problem fixed; otherwise you'll just have later errors/failures. However, in this specific case apparently the root cause of the failure is unfixable due to no network access to the resource required. (Although I'd have expected a better, more specific error message from go get in that case; perhaps instead of just blocking an HTTP request has been replaced with a page that contains some data that confuses go get).

First, what is go get golang.org/x/tools/cmd/goimports trying to do. The documentation says it "downloads and installs the packages named by the import paths, along with their dependencies."

It's important to realize two things: one, it does this by fetching the package sources (usually by cloning a VCS repository) and then building the package(s)/program(s); and two, if the source directory appears to already exist it does not fetch or update anything. The later can cause issues if something undetected went wrong with a previous go get as you'll be stuck with the "corrupt" sources. If this happens you can either use the -u flag to attempt an update or you can remove the old/corrupt/incomplete directory from your GOPATH and try a fresh go get.

The documentation also describes how it interprets "remote import paths", basically trying to match it to a know code hosting service (GitHub, Bitbucket, etc) or to a remote version control system repository (git, mercurial, etc) or doing an HTTPS/HTTP request and looking for a meta name="go-import" … tag.

You can see some of this by using go get -v:

# go get -v golang.org/x/tools/cmd/goimports
Fetching https://golang.org/x/tools/cmd/goimports?go-get=1
Parsing meta tags from https://golang.org/x/tools/cmd/goimports?go-get=1 (status code 200)
get "golang.org/x/tools/cmd/goimports": found meta tag main.metaImport{Prefix:"golang.org/x/tools", VCS:"git", RepoRoot:"https://go.googlesource.com/tools"} at https://golang.org/x/tools/cmd/goimports?go-get=1
get "golang.org/x/tools/cmd/goimports": verifying non-authoritative meta tag
Fetching https://golang.org/x/tools?go-get=1
Parsing meta tags from https://golang.org/x/tools?go-get=1 (status code 200)
golang.org/x/tools (download)
golang.org/x/tools/go/ast/astutil
golang.org/x/tools/imports
golang.org/x/tools/cmd/goimports

In this case go get does an HTTPS request to golang.org to discover the location of the source and effectively does¹:

mkdir -p $GOPATH/src/golang.org/x/tools
git clone https://go.googlesource.com/tools $GOPATH/src/golang.org/x/tools
go build golang.org/x/tools/cmd/goimports

It found that golang.org/x/tools/cmd/goimports is part of golang.org/x/tools and downloads that whole repository by doing a git clone and then building the goimports command and it's two dependant packages (which conveniently are within the same repository, otherwise there would be multiple clone/download steps). The results are put under $GOPATH/bin (and $GOPATH/pkg/$GOOS_$GOARCH).

So if go get isn't working or is blocked from accessing golang.org the first thing to try would be the above commands. Failing that you could substitute a mirror (e.g. git clone https://github.com/golang/tools.git $GOPATH/golang.org/x/tools), or you could otherwise download the contents of the repository from somewhere and unpack them into the correct location within your GOPATH, e.g. perhaps something like:

curl https://github.com/golang/tools/archive/master.zip
unzip -d $GOPATH/src/golang.org/x/tools master.zip

Once the source for all dependant packages is "somehow" put into GOPATH/src you should be able to go build {path}. The important part here is to make sure the sources are where the go tools expects them to be, that is, under $GOPATH/src/{go/import/path}.

If only some domains (such as golang.org) are blocked you could do this for only the blocked repositories and then do go get golang.org/x/tools/cmd/goimports; since go get won't try to fetch existing directories this should only fetch any missing dependencies before building everything.

¹ Note, these examples assume the common case of GOPATH being a single directory. If instead your GOPATH is multiple directories, e.g. GOPATH=$HOME/go.public:$HOME/go.private, then you'd need to substitute the first path component of your GOPATH.

Dave C
  • 7,729
  • 4
  • 49
  • 65
  • 1
    Note this is all pre-modules. If using [Go 1.11+ with modules](https://github.com/golang/go/wiki/Modules) things work slightly differently and the best work around for issues fetching would probably be setting `GOPROXY` and perhaps using a custom proxy. – Dave C Feb 04 '20 at 14:35
  • `export GOPROXY=https://gocenter.io` was the answer that worked for me, thanks @dave-c – Harrison Mar 25 '21 at 19:44
0

Try these 3 approaches:

  1. On Windows, set the proxies in Internet Explorer.
  2. Add http_proxy and https_proxy to your environment variables.
  3. If you are using git, edit the .gitconfig file like this:
[http]
   proxy = "your proxy"
smwikipedia
  • 61,609
  • 92
  • 309
  • 482