bashgoenvironment-variablesubuntu-14.04.profile

How do I fix my GOROOT and GOPATH variables to run 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.


Solution

  • 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.