我遇到了一些问题,无法获取golang.org/x/tools/cmd/goimports。

huangapple go评论79阅读模式
英文:

I am having trouble getting golang.org/x/tools/cmd/goimports

问题

当我获取 golang.org/x/tools/cmd/goimports 时,我遇到了以下错误:

package golang.org/x/tools/cmd/goimports: 无法识别的导入路径 "golang.org/x/tools/cmd/goimports"

我尝试从源代码编译 goimports,所以我从以下链接下载了它:

http://github.com/golang/tools.git

https://github.com/bradfitz/goimports

请问如何编译它?

英文:

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 ?

答案1

得分: 14

这个答案将提供一种解决因网络阻塞而导致go get失败的方法。
请注意,如果您的Go安装存在配置问题(例如GOPATH不正确),则不应该绕过此类失败,而应该修复底层问题;否则,您将在以后遇到错误/失败。然而,在这种特定情况下,失败的根本原因似乎是由于无法访问所需资源的网络访问受阻。 (尽管我本来期望go get在这种情况下提供更好、更具体的错误消息;也许不仅仅是阻止了一个HTTP请求,而是用一个包含某些数据的页面替换了它,这些数据会使go get混淆)。

首先,让我们了解一下go get golang.org/x/tools/cmd/goimports的作用。文档中说它“下载并安装由导入路径指定的包,以及它们的依赖项”。

有两件事很重要要明白:一是它通过获取包的源代码(通常是通过克隆版本控制系统存储库)然后构建包/程序来实现这一点;二是,如果源代码目录已经存在,它将不会获取或更新任何内容
如果之前的go get出现了问题,导致源代码“损坏”,这可能会引发问题。如果发生这种情况,您可以使用-u标志尝试更新,或者您可以从GOPATH中删除旧的/损坏的/不完整的目录,然后尝试进行全新的go get

文档还描述了它如何解释“远程导入路径”,基本上是尝试将其与已知的代码托管服务(GitHub、Bitbucket等)或远程版本控制系统存储库(git、mercurial等)匹配,或者执行HTTPS/HTTP请求并查找meta name="go-import" ...标签。

您可以使用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

在这种情况下,go getgolang.org执行了一个HTTPS请求,以发现源代码的位置,并实际上执行了以下操作:

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

它发现golang.org/x/tools/cmd/goimportsgolang.org/x/tools的一部分,并通过执行git clone下载整个存储库,然后构建goimports命令及其两个依赖包(方便的是,它们都在同一个存储库中,否则将需要多个克隆/下载步骤)。结果被放在$GOPATH/bin(和$GOPATH/pkg/$GOOS_$GOARCH)下。

因此,如果go get无法工作或被阻止访问golang.org,首先尝试上述命令。
如果失败,您可以替换为镜像(例如git clone https://github.com/golang/tools.git $GOPATH/golang.org/x/tools),或者您可以从其他地方下载存储库的内容,并将其解压到GOPATH中的正确位置,例如:

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

一旦所有依赖包的源代码“以某种方式”放入GOPATH/src中,您应该能够go build {path}
重要的是确保源代码位于go工具期望的位置,即$GOPATH/src/{go/import/path}下。

如果只有某些域名(如golang.org)被阻止,您可以仅对被阻止的存储库执行上述操作,然后执行go get golang.org/x/tools/cmd/goimports;由于go get不会尝试获取现有目录,这应该只会在构建之前获取任何缺失的依赖项。

<sub>¹ 注意,这些示例假设GOPATH是单个目录的常见情况。如果您的GOPATH是多个目录,例如GOPATH=$HOME/go.public:$HOME/go.private,那么您需要替换GOPATH第一个路径组件。</sub>

英文:

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=&quot;go-import&quot; … 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 &quot;golang.org/x/tools/cmd/goimports&quot;: found meta tag main.metaImport{Prefix:&quot;golang.org/x/tools&quot;, VCS:&quot;git&quot;, RepoRoot:&quot;https://go.googlesource.com/tools&quot;} at https://golang.org/x/tools/cmd/goimports?go-get=1
get &quot;golang.org/x/tools/cmd/goimports&quot;: 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.

<sub>¹ 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.</sub>

答案2

得分: 0

尝试以下三种方法:

  1. 在Windows上,在Internet Explorer中设置代理。
  2. http_proxyhttps_proxy添加到您的环境变量中。
  3. 如果您正在使用git,请像这样编辑.gitconfig文件:
[http]
   proxy = "your proxy"
英文:

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"

huangapple
  • 本文由 发表于 2015年8月7日 07:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/31867347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定