November 13, 2009

Install Go Compiler on Ubuntu


Google released a new 'experimental' programming language called Go. It's simple, fast, safe, concurrent and fun. All of these were hard to find in the same programming language. And also, Go is open source.

The installation guide for Go is pretty straightforward. Still, I am going to rewrite that specifically for 32-bit Ubuntu (9.10) system.

First you need the set the environment variables. Open the .bashrc file from your home directory. Add the following lines at the end.
export GOROOT=$HOME/go
export GOOS=linux
export GOARCH=386 


Next, install mercurial. To do that use the command
$ sudo apt-get install mercurial 
You are now ready to download the source code for Go.
$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT
You need to compile the source code. For that you will require gcc, the standard C libraries, the parser generator Bison, make and the text editor ed installed. Run the following command. It will install these things if they are not already installed.
$ sudo apt-get install bison gcc libc6-dev ed make
Create the directory bin in your home folder. You can use the command:
$ mkdir $HOME/bin 


Make sure the $HOME/bin directory is in your path. You may have to just log off and re-login after you create the bin directory. The bin directory in your home will be automatically added to $PATH.

To check the contents of PATH variable, use the command
echo $PATH


Now, start the compilation
$ cd $GOROOT/src
$ ./all.bash
That's it. If everything goes well, it will finish by printing
--- cd ../test
N known bugs; 0 unexpected bugs
N will vary from release to release.

Now lets write a simple program.
package main
import "fmt"
func main() {
  fmt.Printf("Hello, World\n")
}
Save the program in a file hello.go.
Compile, link and run the program.

To compile:
$ 8g hello.go
To link:
$ 8l hello.8
And to Run:
$ ./8.out 
That should print the phrase "Hello, World".

To learn more about the Go Programming language read the tutorial here http://golang.org/doc/go_tutorial.html

0 comments:

Post a Comment