Go to Go - An Introduction to Go language

First of all Happy New year to all. Great now we should be having a good start for the year 2014. So coming to the point. Being a developer I have worked on server languages and tools, naming ruby, rails, javascript, titanium, mongodb a few.

I also heard about some good languages coming out of womb, some looked very promising like go, elixir(runs on Erlang virtual machine) and julia. I started exploring, because of my curiousity and really hooked to them. Go is one of them that I also dared to use in production, and to my surprize it kept its promise. This special series will cover go language(from whichever perspective).

Go is a C like language, developed with keeping current system requirements and capabilities in mind, it has a robust memory management, garbage colletion, concurrency(via goroutine or coroutines actually). If you would like to read into details look into Go and wiki. It is not a OOPS language as paradigm is sifting back to functional and imperative languages.

Install it

Go get package for your operating system and install it from here, export go executables to path.

you should be able to check if go has installed correctly by typing following in console

go version
#=> go version go1.2 darwin/amd64
# to list all commands available and get help
go help

#one should see something like
Go is a tool for managing Go source code.

Usage:

  go command [arguments]

The commands are:

    build       compile packages and dependencies
    clean       remove object files
    env         print Go environment information
    fix         run go tool fix on packages
    fmt         run gofmt on package sources
    get         download and install packages and dependencies
    install     compile and install packages and dependencies
    list        list packages
    run         compile and run Go program
    test        test packages
    tool        run specified go tool
    version     print Go version
    vet         run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

    c           calling between Go and C
    gopath      GOPATH environment variable
    importpath  import path syntax
    packages    description of package lists
    testflag    description of testing flags
    testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

Now next step is to setup a GOPATH, GOPATH is a directory where all go code dependencies; that one installs, resides. It also contains go projects that one builds. So GOPATH has to be set before we move on.

Let’s set it up somewhat like this in bash profile

#export GOPATH=<relative-or-complete-path-to-go-directory>
export GOPATH=/Users/xxx/code/go_source

#Now one should be able to see

Every go project has a directory layout, as mentioned below

$GOPATH
  |
  |--- src/**/*.go # contains source code of go projects
  |
  |--- pkg
  |      |--- $GO_ARCH/*.a #contains go project objects
  |
  |--- bin #contains all binaries of installed go project

Go also has a very interesting third party packages integration tool called go get. It also detects popular vcs tools like git and integrates well with github. Here is an example for getting mongodb driver called mgo

# this will be installed under $GOPATH/src/github.com/xushiwei/mgo
go get github.com/xushiwei/mgo

Now we are all set. So let’s start with our first code. Navigate to GOPATH directory under src create a new directory, name it hello_gopher and then make a file called hello_gopher.go

package main #defining name for currenct package

import "fmt" #way to import a package

func main(){  #function that should be run when invoking this package
  fmt.Println("Hello tiny gopher!")
}
#Now we can run it directly via go run. this command compiles and run given file

go run hello_gopher.go
#=> Hello tiny gopher!
#We can also compile this file and then use it
go build hello_gopher.go
#this will generate an executable with same name as file in this case hello_gopher
#that can be run/invoked directly by calling this as
./hello_gopher
#=> Hello tiny gopher!

Happy Coding ;)