Go:找不到包“;fmt“;错误

Go:找不到包“;fmt“;错误,go,Go,我正在尝试使用go中的一个简单的Hello,World应用程序: package main import ( "fmt" ) func main() { fmt.Println("Hello World!") } 但当我构建并运行它时,会出现以下错误: main.go:5:2: cannot find package "fmt" in any of: /home/user/go/src/pkg/fmt (from $GOROOT) /home/user/go

我正在尝试使用go中的一个简单的
Hello,World
应用程序:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
}
但当我构建并运行它时,会出现以下错误:

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)
以下是
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"

不要使用
apt get
进行安装,只需下载并将其解压缩到您的主目录(或任何目录)

要在终端中获得可用的
go工具
,您必须将
export PATH=$PATH:$GOROOT/bin
行添加到
~/.bashrc

-go工具知道它们应该在哪里,您几乎不需要设置它


您已将其设置为
/home/user/go
,除非在其中构建go,否则该位置不太可能是找到fmt包的正确位置。

运行失败后
转到运行案例。go
命令。我尝试在新的ubuntu 16.04环境中使用apt安装

apt install golang
在此之后,
go run case.go
将按预期运行

我想问题是你不应该设置GOROOT

一旦发现GOROOT不是像“/usr/lib/go-{version}”这样的东西


你已经陷入了错误的境地。立即使用
unset GOROOT
。将其添加到
/etc/profile
~/profile
~/.bashrc
等以获得永久效果。

fmt/fmt.go位于何处?我不确定,我只是
安装了golang
然后将我的GoPath设置为如何取消GOROOT?要取消当前会话的GOROOT,请键入“unset GOROOT”。但是,您可能希望永久取消设置它;在这种情况下,只需从设置变量的配置文件(例如,/etc/profile或~/.bash_profile)中删除设置变量的行。可能值得一提的是,如果在非标准位置安装Go,则应设置GOROOT-最好阅读全文以了解“标准位置”是什么,因为这取决于您安装Go的方式。我同意您的观点,但您应该解释为什么不使用apt get:Go开发正在快速发展,Linux发行版的存储库中很可能有过时的版本。如果您想从软件包中安装go,我建议您使用go on the fly的最新二进制版本构建debian软件包