Sunday, November 22, 2009

go : Part 1

go : a new language by google. Best is its OPEN SOURCE. As it says its fast, simple, fast, secure, c0ncurrent, fun, opensource!

This article covers the
Step 1 : Installation
Step 2 : Hello World program

Step 1 : Installation

Best way is to look in go site

  1. Create two directories in your home namely go and bin. (you can set any path). For simple understanding i have created the directory in home folder. If you choose different path than in place of $HOME write your path like /home/abc/golang/go ...
  2. Open .bashrc file. Normally its at your home. Open $HOME/.bashrc in editor.
  3. Add following lines (assuming u have 32 bit computer).

    export GOROOT=$HOME/go
    export GOARCH=386
    export GOOS=linux
    export GOBIN=$HOME/bin

  4. Open Terminal

    programmer@programmer-laptop:~$
    programmer@programmer-laptop:~$ sudo easy_install mercurial
    [sudo] password for programmer:
    Searching for mercurial
    Best match: mercurial 1.3.1
    Processing mercurial-1.3.1-py2.6-linux-i686.egg
    mercurial 1.3.1 is already the active version in easy-install.pth
    Installing hg script to /usr/local/bin

    Using /usr/local/lib/python2.6/dist-packages/mercurial-1.3.1-py2.6-linux-i686.egg
    Processing dependencies for mercurial
    Finished processing dependencies for mercurial

    programmer@programmer-laptop:~$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT
    programmer@programmer-laptop:~$ sudo apt-get install bison gcc libc6-dev ed make

    programmer@programmer-laptop:~$ cd $GOROOT/src
    programmer@programmer-laptop:~$ ./all.bash
    --- cd ../test N known bugs; 0 unexpected bugs

We are done now ..... We have successfully installed go :)

Lets see what is there in bin directory. There are many executable files. We should see 8g (Compiling), 8l (linking).

The file have an extension .go. So lets create hello.go with following lines

package main
//Says its main package
import "fmt" // import

func main() //Entry Point
{
fmt.Print("Hello, World\n"); //Printf /cout/
}


Open Terminal
programmer@programmer-laptop:~$ cat hello.go

package main
import "fmt"

func main()
{
fmt.Print("Hello, World\n");
}

programmer@programmer-laptop:~$ 8g hello.go
programmer@programmer-laptop:~$ 8l hello.8
programmer@programmer-laptop:~$ 8.out
Hello, World
programmer@programmer-laptop:~$

Simple isn't it !!

8g is go compiler and will create a file with extension .8. To link the file 8l is used and the output goes in to 8.out.

If i want to import more packages with fmt we can do like

package main
import (
"os";
"fmt";
)

To make a variable s of type string

var s string = "" // With Datatype

OR

var s = "" // No Type needed Compiler will decide

OR

s := "" // Initializing and Declaring

// Single Line comment

/*
MULTI LINE COMMENT
*/



Lets try out another program. This will read command line arguments and displays the same. Like echo command

Open text editor and add following lines


package main

import (
"os";
"flag"; // command line option parser
)

var omitNewline = flag.Bool("n", false, "don't print final newline")

const (
Space = " ";
Newline = "\n";
)
/*
We could have done like

const Space = " "
const NewLine = "\n"

(No need of semi colon here)
*/

func main()
{
flag.Parse(); // Scans the arg list and sets up flags
var s string = "";

for i := 0; i <> 0 {
s += Space
}
s += flag.Arg(i);
}
if !*omitNewline {
s += Newline
}
os.Stdout.WriteString(s);
}

Carefully see the if condition and for loop, its bit syntactically different from what we have seen in c, c++, Java. No need to add condition or for loop parameter in (). But the body should be enclosed in {}.

See the usage of i:=0. No forward declaration is required as := will initialize and assign the value to i and compiler will know what to do with i.

Seems Simple.. But still this is the very very basic things rather not basic also there are lot many features to be seen.

Keep looking will publish the next post with more information and what more in go.