How to properly install Go Language in Ubuntu?

I'm trying follow these instructions to install Drive but I can't get Go to work properly.

I keep getting the error:

go tool: no such tool "6g"

When I try to use go get [url]. I also get this error when trying to run a hello world script (to check that Go had installed correctly).

I've tried installing the individual programs recommended in this post (golang-go.tools gccgo-go) regarding fixing this error but the problem persists.

How can I get the 6g tool?

Here are the results of go env:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/user/go"
GORACE=""
GOROOT="/usr/share/go"
GOTOOLDIR="/usr/share/go/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"

The problem is your GOROOT. It should be set to /usr/lib/go (not /usr/share/go).

export GOROOT=/usr/lib/go

Installation

  1. Update.

    sudo apt-get update 
    
  2. Install Go language.

    sudo apt-get install golang
    
  3. Set GOPATH to your home folder. After this action, you can use command echo $GOPATH to check the go path location.

    export GOPATH=$HOME/gopath
    
  4. For the GOPATH, it is not required to define it. But you can still define it.

    # Get the go installation root path.
    go env GOROOT
    
    # Set the result of `go env GOROOT` to system variable GOROOT.
    # In this example, the result of `go env GOROOT` is `/usr/lib/go-1.6`
    export GOROOT=/usr/lib/go-1.6
    

Checking Go Environment Variables

  1. Use command go env and you will get:

    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/home/casper/gopath"
    GORACE=""
    GOROOT="/usr/lib/go-1.6"
    GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
    CXX="g++"
    CGO_ENABLED="1"
    
  2. Now you can see that:

    a. GOPATH is in /home/casper/gopath.

    b. GOROOT is in /usr/lib/go-1.6.

References

  • Install package golang
    https://github.com/golang/go/wiki/Ubuntu

  • You don’t need to set GOROOT, really
    http://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really

  • Getting Started - The Go Programming Language
    https://golang.org/doc/install