Skip to content

Go Programming

Go Programming Language Intro Tutorial

The Go programming language was recently released by Google as a systems language that focuses on the ease of development, fast compilation and concurrent computing. It may very well be the ideal language for the development of a custom web server and distributed computing in the near future. But we don’t know it for sure because it’s still being tested out. Anyway, it doesn’t hurt to have a little fun of our own.

I am going to walk you through from building the Go compiler environment to compiling your first Go program – “Hello World!”. Moderate knowledge and skills of Linux server administration and programming is preferred. To make things simple, this tutorial assumes you are logging in as root (so you don’t have to use ‘sudo’ to do stuff). Here we go.

First you will need a Linux server set up. You can either install the distro on your own computer or simply order a VPS. I use Debian 5.0 on a Rackspace Cloud server.

Log in via SSH to the server, add these lines (environment variables Go compilers will need to work properly) to.bash_profile at root of your home directory:

export GOROOT=$HOME/go

export GOARCH=386

export GOOS=linux

export GOBIN=$HOME/bin

PATH=$PATH:$GOBIN

GOARCH=386 is for 32 bit architecture, if your server’s CPU is 64 bit architecture, use:

export GOARCH=amd64

Instead. Then make them all in effect by running:

source.bash_profile

Chances are you have not had the ‘hg’ command installed. Run:

aptitude install mercurial

To install it. Use the ‘hg’ command to fetch the sources from Google code:

hg clone -r release $GOROOT

Which would simply download release version of the Go sources to the directory you just set at $GOROOT, /home/username/go. Now we will proceed to install the necessary compilers (GCC), C libraries and related utilities to compile the Go cause’ it’s written in C:

aptitude install bison gcc libc6-dev ed make

Wherein bison is the parser generator, ed is a text editor and make is the compiler. If all goes well, you can compile Go now:

cd $GOROOT/src

make all

The compiler will perform basic tests at the end of the compilation.

After all is finished. Let us write and compile our first Go program. While you can use ed the editor we just installed to write and save the program (before compilation, the program is just a common text file), I will be using ‘nano’ the Windows Notepad cousin in Linux to do the demo. Open nano editor by:

nano hi.go

And type in as follows:

package main

import “fmt”

func main() {

fmt.Printf(“Hello, world!”)

}

Press ‘ctrl+x’ and ‘y’ to save the file to hi.go. Now that you have a Go source file hi.go, we can compile it by:

8g hi.go

Which would output a file named hi.8 that must be linked by:

8l hi.8

Which would output yet another file named 8.out that is a binary executable – you have done it. Simply run the compiled file 8.out by:

./8.out

And it should print in a new line: Hello, world!

Note though that if your server’s architecture is 64 bit, the compiler is ‘6g’ (for 64 bit systems) instead of ‘8g’ (for 32 bit systems), the linker is ‘6l’ instead of ‘8l’ and the output file would be hi.6 and 6.out. You get the idea.

You can find out more tutorials and tips about the Go programming language at Go Tutorials and Tips. Make sure you enter this Rackspace Cloud promo code to obtain a discount in case you are going with them for hosting.

Article Source: http://EzineArticles.com/?expert=Yang_Yang

Be Sociable, Share!
    The following two tabs change content below.

    Add Your Comment (Get a Gravatar)