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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?