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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题