Google’s New Programming Language GO

Introduction.

In this blog post we will go through the installation of the Go programming language tool-chain on Linux & most probably talk about the basic programming constructs of this language.

I am using an Intel Core 2 Duo processor and Linux flavor is Ubuntu 9.10 code named Karmic Koala.

The official installation procedure can be found at http://golang.org/doc/install.html. We will be following the same procedure but I will be explaining few of the steps in between so that you don’t get lost.

Step 1. Setting Environment Variables

First thing we need to do is to set the environment variables in .bashrc in your home directory. You can read more about bash & .bashrc at http://en.wikipedia.org/wiki/Bash#Startup_scripts.

On your Linux desktop go to the Applications menu → Accessories → Terminal. This will bring up the terminal. Type “cd ~” & hit enter to make sure you are in your home directory.

Now type gedit .bashrc in the terminal.

This will open up the .bashrc file in gedit, scroll down to the end add the following text

# Environment variables for Go programming language.

export GOROOT=$HOME/go

export GOOS=linux

export GOARCH=386

export GOBIN=$HOME/bin

Save file & close the document. Type exit in the terminal to exit. Once again go back to the application menu and click on the terminal by following the route specified above.

Type env | grep ‘^GO’ to check environment variables are set correctly.

If everything went well you will see something like this

click to enlarge

Installing Mercurial

Once you the environment variables are set correctly type

sudo apt-get install mercurial

The package if not installed will be downloaded & installed or upgraded depending upon your system.

Screenshot
click to enlarge

You can read more about mercurial here: http://en.wikipedia.org/wiki/Mercurial

Checking Out From The Repositories

Once mercurial is installed type the following command on the terminal to checkout the latest code from repositories

hg clone -r release https://go.googlecode.com/hg/ $GOROOT

Wait for a while until it gets downloaded

Installing Other Required Components

On getting the prompt back type

sudo apt-get install bison gcc libc6-dev ed

If the above-mentioned programs are not installed on your system they will be downloaded and installed.

Installing Go

Once you get the prompt back & above mentioned programs are successfully installed on your system, type:

cd $GOROOT/src

And then enter the following command

./all.bash

Installation process will begin & if all goes well you will see screen like this.

Screenshot-2
click to enlarge

Congratulations you have successfully installed the Go language tool-chain, now its time to get our hands dirty with a little programming.

Hello World in Go

Now type:

cd ~

to go to the home directory now create a new folder by typing

mkdir google-go

type:

cd google-go

to go to the newly created directory.

Now type:

Gedit

& hit enter

Enter the following code in gedit.

Example 1

package main

import fmt “fmt”

func main(){

fmt.Printf(“Hello World from Go programming language\n”)

}

That’s it, save the document as hello.go and exit the gedit.

On the terminal type:

8g hello.g

to compile the source. If all goes well you’ll get to the prompt with anything showing.

Now type 8l hello.8 (thats character ‘l’ after 8 not numeric 1), this command will link the required libraries “fmt “ in our case which imported in our program.

And to run the program type ./8.out

Congratulations you have compiled your first program in Google’s new programming language Go. Be proud of yourself.

Try changing this program use different language constructs. Below is the yet again a simple program which will get you started. I cannot go over every language construct so feel free to visit the official Go website. You’ll grasp the concepts very quickly if you have any prior programming experience.

Example 2

package main

import fmt “fmt”

func main(){

var i int = 1;

var stng string;

var j [10]int;

stng = “A string”;

fmt.Printf(“The String contains : %s, integer contains : %d, and array %i \n”, stng , i, j);

for k := 0; k<10; k++{

fmt.Printf(“%d Checking for loop\n”, k);

}

}

You should have noticed that Go is a C based language & C/C++ programmer will have no trouble using this language.

Below is one hour Google Tech Talk video hope you will enjoy it.

You can always visit the official website http://golang.org/ to learn more.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s