[英]Go: cannot find package “fmt” error
I'm trying to follow a simple Hello, World
application in go:我正在尝试遵循一个简单的Hello, World
应用程序:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}
But when I build and run it, I get the following error:但是当我构建并运行它时,我收到以下错误:
main.go:5:2: cannot find package "fmt" in any of:
/home/user/go/src/pkg/fmt (from $GOROOT)
/home/user/gocode/src/fmt (from $GOPATH)
package test_program
imports runtime: cannot find package "runtime" in any of:
/home/user/go/src/pkg/runtime (from $GOROOT)
/home/user/gocode/src/runtime (from $GOPATH)
Here is the ouput of go env
:这是go env
的输出:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/user/gocode"
GORACE=""
GOROOT="/home/user/go"
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"
Unset GOROOT - the go tools know where they are supposed to be and you rarely need to set it. 取消设置 GOROOT - go 工具知道它们应该在哪里,你很少需要设置它。
You've set it to /home/user/go
and unless you build go in there that is unlikely to be the right place to find the fmt package.您已将其设置为/home/user/go
并且除非您在那里构建/home/user/go
,否则它不太可能是找到 fmt 包的正确位置。
Don't use apt-get
for installing go , just download the binaries and extract them to your home (or any) directory.不要使用apt-get
安装go ,只需下载二进制文件并将它们解压缩到您的主(或任何)目录。
To get the go tool
available in your terminal, you have to add the export PATH=$PATH:$GOROOT/bin
line to your ~/.bashrc
.要在终端中使用go tool
,您必须将export PATH=$PATH:$GOROOT/bin
行添加到~/.bashrc
。
After failed run go run case.go
command.失败后运行go run case.go
命令。 I try to use apt installation in a new ubuntu 16.04 environment.我尝试在新的 ubuntu 16.04 环境中使用 apt 安装。
apt install golang
after this, the go run case.go
will run as expected.在此之后, go run case.go
将按预期运行。
I guess the problem is you should never set the GOROOT .我想问题是你永远不应该设置 GOROOT 。
Once you find the GOROOT is not something like "/usr/lib/go-{version}",一旦你发现GOROOT不像“/usr/lib/go-{version}”,
you already in the wrong situation.你已经在错误的情况下。 use unset GOROOT
immediately.立即使用unset GOROOT
。 add it to /etc/profile
or ~/profile
or ~/.bashrc
etc. for permanent effection.将它添加到/etc/profile
或~/profile
或~/.bashrc
等以永久生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.