Go Lang - Quick Setup

on Saturday, January 25
I had almost written this post two weeks back using the wordpress mobile client on my #samsung note stylus (handwriting mode) during a train travel. Though the client indicated that the post is saved as draft on the mobile client, it never uploaded to the server and I lost all my content. Coming back to the post, the intention of this post is for someone who has decent knowledge of C/C++ programming and wants to give a try to #golang.
Lets start with the installation, setting up environment variables and then some variable definition methods.

Go is new programming language developed  by google and is also supported  by the Google App Engine (GAE). You can also you to develop tools to run on your linux or  windows platform. The compiled binary/executable code can run on the other machines without any shared or dependency libraries. One of the impressive and most admired aspect of golang is its concurrency (Do a google you will get a lot of hits).

Installation (Linux)

The steps below are given for linux (See  golang's homepage)
  • Untar the latest go1.2.linux-386.tar.gz  (though Ubuntu has golang in its repository)
  • Assuming ~/go contains those files , set and update your environment variables
  • export PATH=$PATH;~/go/bin 
  • export GOROOT=~/go
Next, important step (dont ignore) is to have one location called as workspace location for all the developments and dependency packages etc.

~$ mkdir -p goproj/bin goproj/src goproj/pkg
Next set environment variable :

~$ export GOPATH=~/goproj
~$ export GOROOT=~/go



The environment variable can be set permanently in your ~/.bashrc  file

This helps the different editors and IDE to do auto-complete for the packages that you import or your own packages. For learning any new language, unless you have a suitable IDE, its takes too much time to learn. I recommend to install SublimeText  (along with GoSublime Package) or LiteIDE (download). Lets create the first example - "helloworld.go".  The ~/goproj/src folder contains all the source codes. Ignore for a while -  It contains all sources imported from github , google code etc (see list here). Inside the ~/goproj/src create a folder of yourname or nickname which you will refer ( e.g. ~/goproj/src/wiless ). Think of this like a namespace under which you can refer to all your packages (library).

First example (helloworld.go)

Create the file helloworld.go  :   ~/goproj/src/yourname/example1/helloworld.go
package main
import (
 "fmt"
)

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

Build & Run

 
~/goproj/src/yourname/example1$ go build helloworld.go
~/goproj/src/yourname/example1$ ./helloworld
Hello World

Minimal Lines in the Codes

  • package main : Indicates the "go build" tool that it has main routine to be the entrant function to run
  • import ( "fmt" ) : Imports the package "fmt" in the "pkg" subfolder in "GOROOT and GOPATH"
  • fmt.Print : Calls the public function "Print" defined in the "fmt" package. All the public functions in a package starts with a capital letter and private functions are with small letters.
In the next article, I shall post about variables in Go.