How do I fix my GOROOT and GOPATH variables to run go?

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

How do I fix my GOROOT and GOPATH variables to run go?

问题

操作系统:Ubuntu 14.04

.bashrc 文件中的行(尝试了以下两种方式)

# GoPath(不包括 goroot)
export GOPATH=$HOME/gowork

...

# GoPath(包括 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)

编辑

go env 输出

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 输出

go version go1.3.3 linux/amd64

一个提示

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

英文:

Operating system: Ubuntu 14.04

lines in the .bashrc (tried both of the following)

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

...

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

Note: I tried to set the above environment variables, first in .profile and then in .bashrc as suggested by different people in a different way.

Tried to run the following code

package main

import "fmt"

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

Error message in the terminal

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)

edit

go env output

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 output

go version go1.3.3 linux/amd64

A hint

I remember that the tar I installed from was go1.6.linux-amd64.tar.gz, which means the go version 1.6 should be shown but my terminal is outputting 1.3.3.

答案1

得分: 4

通常情况下,你不需要设置$GOROOT,除非你知道为什么要设置它。

这假设你已经按照正常方式安装了Go。如果你移动了Go二进制文件,或者删除了pkg或src文件夹,那么情况可能会有所不同。

另外,请确保在你打算使用的版本之前,你的$PATH环境变量中没有找到旧版本的Go。

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

通常人们在切换多个Go版本时使用$GOROOT。

请阅读解决方案的注释和聊天记录。

通常人们使用$GOROOT在多个Go版本之间切换。

英文:

You should generally never have to set $GOROOT unless you know why you're setting it.

This assumes a 'normal' installation of Go. If you've moved the go binary around, or deleted pkg or src folders, your mileage may vary.

Also, make sure an older version of go is not found in your $PATH before the version you intend to use.

In this case @Bangash had to delete the old /usr/bin/go and add /usr/local/go/bin to his PATH environment variable.

Usually people use $GOROOT when switching between multiple versions of go.

Please read comments as well as chat for the solution.

huangapple
  • 本文由 发表于 2016年3月30日 11:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/36299375.html
匿名

发表评论

匿名网友

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

确定