In this blog post we will go through the installation of the Go programming language tool-chain on Linux & 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.
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
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.
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.
Once you get the prompt back & above mentioned programs are successfully installed on your system, type:
And then enter the following command
Installation process will begin & if all goes well you will see screen like this.
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
to go to the home directory now create a new folder by typing
to go to the newly created directory.
& hit enter
Enter the following code in gedit.
That’s it, save the document asand exit the gedit.
On the terminal type:
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.
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.
The article was originally published here.