Go在错误的目录中查找包。

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

Go looks for packages in wrong directory

问题

我已经使用Windows msi在我的机器上安装了Go,但是当我尝试运行一个简单的Hello World程序时,像这样:

go run hello.go

我得到了以下错误:

> hello.go:3:8: 无法在任何地方找到“fmt”包:
C:\Go\src\pkg\fmt (来自$GOROOT)

GOROOT中确实找不到这个包,因为它安装在以下目录中:

C:\Go\src(缺少\pkg)

GOROOT被设置为C:\Go(自动设置),C\Go\bin被添加到PATH中。

我的GOPATH被设置为我的工作空间,但似乎没有改变任何东西。

如何修复这个问题?我应该将所有包复制到C:\Go\src\pkg中吗?
我做错了什么吗?

编辑

根据@Volker的要求,这是go env的输出:

set GOARCH=amd64
set GOBIN=
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\aage\gocode
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
英文:

I've used the Windows msi to install Go on my machine, but when I try to run a trivial Hello World, like so:

go run hello.go

I get the following error:

> hello.go:3:8: cannot find package "fmt" in any of:
C:\Go\src\pkg\fmt (from $GOROOT)

In the GOROOT this package can indeed not be found, since it's installed in the following directory:

C:\Go\src (missing the \pkg)

GOROOT is set to C:\Go (automatically), C\Go\bin is added to the PATH

My GOPATH is set to my workspace, but that doesn't seem to change anything.

How can this be fixed? Should I copy all packages to C:\Go\src\pkg?
Did I do anything wrong?

EDIT

As requested by @Volker, here's the output of go env:

set GOARCH=amd64
set GOBIN=
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\aage\gocode
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

答案1

得分: 2

请检查你的 %PATH%:我安装了多个 Go 版本,我看到:

  • %GOROOT%(1.3)\src\pkg 中的 fmtC:\prgs\go\go1.3.2.windows-amd64\src\pkg\fmt\
  • %GOROOT%(1.4)\src 中的 fmtC:\prgs\go\go1.4.windows-amd64\src\fmt\

源代码已经重新组织。
请参阅 "Go 1.4 src/pkg → src"。

这意味着,如果你尝试使用 Go 1.3 进行编译,而 GOROOT 指向的是 Go 1.4 的安装目录,你可能会看到该错误。

如果这样还不行,请卸载并使用 go1.4.windows-amd64.zip 压缩包:将其解压到任意位置,将 GOROOT 指向它,添加 GOROOT/bin,然后一切应该正常工作。

英文:

Check your %PATH%: I have multiple go versions installed and I see:

  • fmt in %GOROOT%(1.3)\src\pkg: C:\prgs\go\go1.3.2.windows-amd64\src\pkg\fmt\
  • fmt in %GOROOT%(1.4)\src: C:\prgs\go\go1.4.windows-amd64\src\fmt\

The sources have been reorganized.
See "Go 1.4 src/pkg → src".

That means you might see that error if you are trying to compile with a go 1.3 while GOROOT points to a Go1.4 installation.

If that doesn't work, uninstall, and use the go1.4.windows-amd64.zip archive: unzip it anywhere you want, point GOROOT to it, add GOROOT/bin and everything should work.

huangapple
  • 本文由 发表于 2014年12月31日 04:40:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/27712244.html
匿名

发表评论

匿名网友

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

确定