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
~$ 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.gopackage 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.