Featured post

Why should I learn Go?


What is unique about GO language?
Here are some of the advantages of GO programming language:
  •          Code runs fast
  •          Garbage collection
  •          Simpler objects
  •          Efficient concurrency

Code runs faster:

Before understanding why GO runs faster, let us know the process of software translation. Basically, we have three broad categories of languages:

           Machine level language
·       Machine level language is a low-level language where instructions are directly executed on the CPU. Machine level instructions are small steps which are straight forward and simple (Ex: ADD, SUBTRACT, MULTIPLY )

Assembly language
·       Assembly language is similar to machine level language but a bit more specific for humans to understand. For example, 100000111001 may be the equivalent opcode representation for ADDITION operation in machine language. Whereas in assembly language you use the word ‘ADD’ to represent the addition operation. Assembly language is a one-to-one mapping to machine language.
       
         High level language (C, JAVA, PYTHON..)
·       High level languages are commonly used by programmers to write complex software tasks. These are much easier to use compared to assembly or machine language since they provide a lot of abstraction.
·       For example, High level languages like C or java use the concept of variables whereas assembly language consist of memory where you can put stuff into and delete it once the operations are done. Basically, there is no concept of ‘type’ in assembly language. Almost all the high-level languages provide features such as ‘if’ statements and ‘loops’. We can implement these features in assembly or machine level language, but they are harder to write than they would be in any high-level language.

‘GO’ is a high-level language. All software must be translated into machine language of the processor to be executed. The translation step can be carried out in one of the two ways:
  •        Compilation
  •        Interpretation

Compilation:
Compiled language is a language in which the translation of high level code to machine code happens one time before we execute the code. In high level languages like C, C++, Java(partially) and GO, we have a compiler which compiles the code which results in an executable (which is usually machine code). So, when we run the code, we are just running the machine language instructions directly on the CPU.

Interpretation:
The other way to execute a high-level language program is through an interpreter. In interpreted languages, the translation occurs when the instructions are being executed. For example, in languages like python, statements have should be translated by the interpreter during execution which slows the execution time. So, every time we run a program, the translation occurs. We consider Java as an interpreted language even though it has a compiler. This is because the compiled code is not directly machine code (It’s called byte code). The byte code still has to be interpreted at runtime to execute the instructions.

Compiled vs Interpreted:
Compiled code is fast because no extra time is needed for translation. There are people who would argue the opposite but generally compiled code is a lot faster. 

 On the other hand, interpreters make coding easier.
  •  The interpreter (itself being a program) helps the programmers to manage certain things such as ‘memory management’. The interpreter will take care of variables and other unused objects.
  •  It can also infer variable types. For example, in python we don’t need to explicitly declare a variable as an integer. The interpreter automatically takes care of the allocation and deallocation of types.

Go is a good compromise between compiled and interpreted languages. It is a compiled language, but it has got some of the great features of an interpreted language. Specifically, it has garbage collector which takes care of the automatic memory allocation and deallocation which I had mentioned earlier.

·       Where should memory be allocated?
·       When should memory be deallocated?

Manual deallocation of memory is hard. If we deallocate memory too early, there is a possibility of false memory access. If we deallocate it too late, a lot of unused memory gets wasted. If you have experience with languages like C, then you might be familiar with manual memory management. Since GO has a garbage collector included, it takes care of the memory management automatically. This might slow down the execution a bit but its still an efficient garbage collector.

Object Oriented:
Go is a weakly object-oriented programming language compared to python, Java or C++

Encapsulation:
Encapsulation groups together data and functions that are related to each other. It lets us create user defined types which is specific to the application. For example, in java we can define a class to encapsulate objects (which contain variables and functions).
    
    Go doesn’t use the term class but the term ‘Structs’ to maintain encapsulation. Again if you are familiar with C language, you definitely know about structures.The implementation of structs in Go is simplified compared to class in other object-oriented programming languages. Go doesn’t have:
  •   Inheritance
  •   Constructors
  •   Generics



Concurrency:
One of the big advantages of GO is its implementation of concurrency. We’ll have to understand the performance limitations before getting into the details of concurrency. A lot of motivation for concurrency comes from the “NEED FOR SPEED”. According to Moore’s law, the number of transistors doubles every 18 months. This was the case previously though it has changed now. 
     More number of transistors leads to higher clock frequencies which increases the performance. Currently the scenario is different. There is a limit on the clock frequencies because of power/temperature constraints. If we increase the number of transistors, more heat is generated in the process of switching on and off the transistors. Air coolers are present to control the heat but their capacity is limited. So, if you clock these things much faster with this density of transistors, the chip may melt. So we cant keep increasing the clock rate.
       So how do we improve the performance without raising the clock rates? One way to do this is “parallelism”. It can be implemented in a number of ways but it is usually done by increasing the number of cores on chips. It helps to perform multiple tasks at the same time on different cores.

Difficulties with parallelism:
  •         When do the tasks start/stop?
  •         What if one task needs data from another task?
  •         Do these tasks conflict in memory?
Concurrent Programming:
Concurrency is the management of multiple tasks at the same time( This means multiple tasks are alive at the same time).  This is a key requirement for large systems where you want things many things to run at simultaneously. Concurrent programming enables parallelism.
·       Management of task execution
·       Communication between tasks
·       Synchronization between tasks

Concurrency in GO:
Go has a lot of primitives built in for concurrency.
·       Goroutines represent concurrent tasks
·       Channels are used to communicate between concurrent tasks
·       Select enables task synchronization

It is advantageous to have concurrency implementation with the current resources we have to make programs run faster.





Comments

Popular posts from this blog

Introduction to Big Data and Hadoop

LocationManager vs GoogleApiClient