# Nice to meet you Go lang

Categories: Blog,General,Go,Language,Programming,Tutorials

## Nice to meet You, GO!

Recently i started to study Go language, the shiny new programming language developed by Google. The general opinion about the language among developers was quite positive so i thought to give it a try.

As a web developer i'm working mainly with dynamic languages like PHP, Ruby, Javascript etc., so for me it was fresh new start to switch back to a language which has a standard AOT (Ahead of Time) compilator, contrary to JIT (Just in Time) compilers, which translate the human readable (high level) code to machine or byte code prior to execution, the compilation time being one of the strong feature of the Go language. Until now this was regarded as one of the great benefits of dynamic languages because the long compile/link step of C++ could be skipped, but with Go this is no longer an issue! Compilation times are negligible, so with Go we have the same productivity as in the development cycle of a scripting or dynamic language. On the other hand, the execution speed of the native code should be comparable to C/C++.

Above the fast compilation time, another main characteristic of Go is that it combines the efficacy, speed and safety of a strongly and statically compiled language with the ease of programming of a dynamic language. Above these, Go is designed for fast parallelization, thread concurrency which are the basic desirables for network programming, Go definitely being designed for highly scalable and lightning fast web servers.This is certainly the great stronghold of Go, given the growing importance of multicore and multiprocessor computers, and the lack of support for that in existing programming languages.

Because it is a statically typed language it's a safe language, giving 100% percent trust for the programmer for incidental and not desired errors to not happen, assigning different types of values to the same variable (redeclaring) being impossible without an error notification. Go is strongly typed: implicit type conversions (also called castings or coercions) are not allowed; the principle is: keep things explicit!

## Text editors and IDE extensions for Go

Having these basic ingredients at our disposal, combined with a powerful editor like IntelliJ IDEA extended with a very versatile Golang Idea Plugin specially developed to support all the syntactical sugar, auto completion, refactoring etc. desired for every IDE, programming in Go is pure fun. I can fully recommend this strong partnership.

I've tried Sublime Text 2 editor with GoSublime plugin, but even if i'm a huge fan of Sublime i think the first option is a better choice. There is a dedicated page at the project site for the current existing editors supporting the language including Eclipse, but considering that till now i was very satisfied with InteliJ editors i sticked with IntelliJ IDEA.

## Installation

The installation of Go is quite straightforward. You only need to follow the directions set on main site: http://golang.org/doc/install, paying special attention to setting up the environment variables. I won't go into details how to do this (you can read this article if you need assistance), i will focus only on two special aspects of the installation process:

1.) The first one is that if you are using Windows environment (i haven't tested on other environments) and want to use Go with IntelliJ IDEA be careful to download the binary code, extract it and copy into the root folder of Go installation, otherwise it's not possible to associate the project to the Go SDK. If everything went well, you should see something like this:

...otherwise if you install with MSI installer, the IDE won't be able to locate and setup the SDK.

2.) Another special attention which you have to pay is to set up the Go environment variables correctly. This means either you specify the GOROOT, GOOS, GOBIN variables per project or you set up permanently by including into the Windows default environment variables either by using the SET command line or simply by editing the existing environment variables on Control Panel -> System and Security -> System -> Advanced System Settings -> Advanced.

However on each project you need to setup the GOPATH variable (which need to be associated to the root of the project folder!!) by running the SET GOPATH="root\to\the\project\folder" command. This is necessary if you wish to run the build or install go commands in order to create executable files or to build the source packages.

## Quasi torture test

After setting up the necessary prerequisites I've started to play with different type of function invocations Go can handle, one of the most referred in every torture test being the recursive function. I came up with three different methods to calculate the factorial of the first 40 numbers and measured the execution time for each one separately.

As you can see from examples below the calculation/execution times are really impressive. The first method is the traditional one. It's execution time is the worst between the three. The second method is a demonstration how Go can handle closure functions and how can solve the same problem more elegantly. The last one is the best one using the memoization technique. What i really liked is how elegantly Go handle the closure function, being possible to define even a function as a return statement. So below are the methods, you can test it yourself by following the steps defined above.

## Conclusion

This is the first post from - hopefully - a series of Go related entries, so along the way tinkering with the language, i'm aiming to publish more views and thoughts. In the current phase i wanted only to share my first impression and steps necessary to install Go with a IntelliJ IDEA on Windows environment.

package main

import (
"fmt"
"time"
)

const LIM = 41
var facts [LIM]uint64

func main() {
fmt.Println("==================FACTORIAL==================")
start := time.Now()
for i:=uint64(0); i < LIM; i++ {
fmt.Printf("Factorial for %d is : %d \n", i, Factorial(uint64(i)))
}
end := time.Now()
fmt.Printf("Calculation finished in %s \n", end.Sub(start)) //Calculation finished in 2.0002ms

fmt.Println("==================FACTORIAL CLOSURE==================")
start = time.Now()
fact := FactorialClosure()
for i:=uint64(0); i < LIM; i++ {
fmt.Printf("Factorial closure for %d is : %d \n", uint64(i), fact(uint64(i)))
}
end = time.Now()
fmt.Printf("Calculation finished in %s \n", end.Sub(start)) //Calculation finished in 1ms

fmt.Println("==================FACTORIAL MEMOIZATION==================")
start = time.Now()
var result uint64 = 0
for i:=uint64(0); i < LIM; i++ {
result = FactorialMemoization(uint64(i))
fmt.Printf("The factorial value for %d is %d\n", uint64(i), uint64(result))
}

end = time.Now()
fmt.Printf("Calculation finished in %s\n", end.Sub(start)) // Calculation finished in 0ms
}

func Factorial(n uint64)(result uint64) {
if (n > 0) {
result = n * Factorial(n-1)
return result
}
return 1
}

func FactorialClosure() func(n uint64)(uint64) {
var a,b uint64 = 1, 1
return func(n uint64)(uint64) {
if (n > 1) {
a, b = b, uint64(n) * uint64(b)
} else {
return 1
}

return b
}
}

func FactorialMemoization(n uint64)(res uint64) {
if (facts[n] != 0) {
res = facts[n]
return res
}

if (n > 0) {
res = n * FactorialMemoization(n-1)
return res
}

return 1
}