【问题标题】:How do I fix my GOROOT and GOPATH variables to run go?如何修复我的 GOROOT 和 GOPATH 变量以运行 go?
【发布时间】:2016-07-17 21:33:40
【问题描述】:

操作系统:Ubuntu 14.04

.bashrc 中的行(尝试了以下两种方法)

# GoPath (without goroot)
export GOPATH=$HOME/gowork

...

# GoPath (with goroot)
export GOROOT=/usr/local/go
export GOPATH=$HOME/gowork

注意:我尝试设置上述环境变量,首先在 .profile 中,然后在 .bashrc 中,按照不同人以不同方式提出的建议。

尝试运行以下代码

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
}

终端中的错误信息

go run helloworld.go 
helloworld.go:3:8: cannot find package "fmt" in any of:
    /usr/local/go/src/pkg/fmt (from $GOROOT)
    /home/arif/gowork/src/fmt (from $GOPATH)
package runtime: cannot find package "runtime" in any of:
    /usr/local/go/src/pkg/runtime (from $GOROOT)
    /home/arif/gowork/src/runtime (from $GOPATH)

编辑

去环境输出

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/arif/gowork"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

去版本输出

go version go1.3.3 linux/amd64

提示

我记得我安装的 tar 是 go1.6.linux-amd64.tar.gz,这意味着应该显示 go 版本 1.6,但我的终端输出的是 1.3.3。

【问题讨论】:

  • stackoverflow.com/a/21012349/2020002 为我工作,你试过了吗?
  • 我建议删除GOROOT
  • @OliverQueen ,在我的情况下不起作用,给我以下错误( go: cannot find GOROOT directory: /usr/lib/go )
  • @OliverQueen 我尝试了两种方式,离开 GoRoot,然后删除它。
  • 你能显示命令go envgo version的输出吗?

标签: bash go environment-variables ubuntu-14.04 .profile


【解决方案1】:

除非您知道为什么要设置它,否则您通常永远不必设置 $GOROOT

这假设 Go 的“正常”安装。如果你移动了 go 二进制文件,或者删除了 pkg 或 src 文件夹,你的里程可能会有所不同。

另外,请确保在您打算使用的版本之前的 $PATH 中没有找到旧版本的 go。

在这种情况下,@Bangash 必须删除旧的 /usr/bin/go 并将 /usr/local/go/bin 添加到他的 PATH 环境变量中。

Usually people use $GOROOT 在多个 go 版本之间切换时。

请阅读 cmets 以及聊天以获取解决方案。

【讨论】:

  • 当我删除 $GOROOT 变量时,它给了我以下错误(go: cannot find GOROOT directory: /usr/lib/go)
  • 按照 golang.org 的建议,我已将其安装到默认位置 /usr/local/go
  • 我从 .profile 和 .bashrc 中删除了之前安装的 go 及其所有变量,然后将新的安装到 usr/local/go 并按照问题中的说明设置变量跨度>
  • @Bangash : 提供go env 的输出,否则没人能帮你。
  • 是的,我将使用 go env 的输出来编辑问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
相关资源
最近更新 更多