Installing golang in ubunut

Installing Golang in Ubunut

Download Golang

For 32-bit systems, run:

wget https://storage.googleapis.com/golang/go1.3.3.linux-386.tar.gz tar -xf go1.3.3.linux-386.tar.gz 

For 64-bit systems, run:

wget https://storage.googleapis.com/golang/go1.3.3.linux-amd64.tar.gz tar -xf go1.3.3.linux-amd64.tar.gz

Move Directory

Upon extraction, you will get a directory named go. Move this folder to /opt/ (or a directory of your choice).

mv go /opt/

If you decide to put it into a different directory, remember to change the directory path accordingly in the next couple of steps.

Add to Environment Path

For everything to work like it should, you will need to do several things. Create two system variables called GOROOTand GOPATH. These two variables will be used by Golang itself when building your application. You will also need to create a directory to store any dependencies being pulled.

mkdir /opt/gopkg export GOPATH="/opt/gopkg"  
export GOROOT="/opt/go"

If you wish for the changes to be permanent upon reboot, you will need to run the following commands:

echo export GOPATH=/opt/gopkg >> ~/.bashrc 
echo export GOROOT=/opt/go >> ~/.bashrc 
echo export PATH=$PATH:$GOROOT/bin:$GOPATH/bin >> ~/.bashrc 

Now you will need to add these to your PATH variable. This can maybe put on the directory where you are is sitting

export PATH=$PATH:$GOPATH/bin:$GOROOT/bin

Verify Installation

Check installation by running the go command. When you run the command, you will see a list of options. Create a file called test.go and populate it with the following code:

package main import "fmt"   func main(){
 fmt.Printf("Hello world\n"); 
} 

You can run this script by typing the command go run test.go. The message Hello world will be printed to your terminal.

The message Hello world will be printed to your terminal.