dongnei3634 2016-03-30 03:55
浏览 770
已采纳

如何修复GOROOT和GOPATH变量以运行go?

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条回答 默认 最新

  • dongqiang4986 2016-03-30 04:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义