go.png

The Go Programming Language

04/November/2017

Go (Golang) is an open source programming language from the house of Google. According to ‘Rob Pike- Co-designer of Go’, the language was developed as an answer to some of the problems they faced internally in developing software infrastructure at Google. It was an attempt to combine the advantages of dynamic languages like python with performance and security advantage of the compiled language like C. Go is a statically typed, compiled languages like C++ and Java. And it follows the simplicity of ‘C’ in syntax and semantics with some additional features. Though the Idea was conceived in 2007, the first release that is Go 1.0 came in March 2012. The language became more prominent after many Blockchain platforms started to use it for developments.


The Features of Go

Go is an efficient choice for Web servers and Networking, command line tools and scripts. Understanding the compulsive force behind the introduction of a programming language like ‘Go’ will help anyone to comprehend the features of it easily. Today most of the systems are using multi-core processor and such an approach increased the speed of processing multifold. But, most of the languages we are using now was developed in an age when single-core processor led the technology. Even though such languages supports multi-threading, it is not much efficient. For example, in Java, each thread reserves 1MB of memory and it creates an overhead in the concurrent execution of these threads. Therefore the multi-threaded approach also failed to the take the advantage of Multiprocessor effectively. It is in this environment a brand new language which is having the advantages of newborn dynamic languages and performance of compiled languages, is introduced. In processor utilization, go uses ‘Goroutines’ instead of threads. Goroutines are very light-weighted, only reserves 2KB of memory for each. They will use additional memory only when it is necessary. A single Goroutine can run a number of OS threads.


Go has an easy and readable syntax and Fast compilation time. As it uses ‘Goroutines’ instead of the thread it has high concurrency support. Since Go is a compiled language the execution time is faster than many similar languages and it has the facility to manage Remote packages also.  But the thing to remember is that Go is purposefully made for a clean and easy way of coding. For that, some of the features which are common in most of the programming languages are excluded from it. They are listed below. 


- Classes and inheritance

- Constructors

- Annotations

- Generics

- Exceptions


But such an exclusion didn’t weaken the performance and utility of the language rather strengthened it.


Go and Blockchain Development

Maybe, Blockchain is the major technology that utilizes the full potential of Go. Most of the Blockchain systems like Ethereum, Hyperledger etc. use go in its implementation.  The interface for running an Ethereum node, ‘geth’ is implemented in Go. 
In Hyperledger, the Chaincodes are written in Go. In addition to the features discussed earlier,  there are some other features made golang as a prior choice among Blockchain geeks.  
 - Since most of the Blockchain projects are open source, the readability and writability of a code is a prime factor, which will encourage newcomers to learn an existing code and make their own contribution. And Go is one of the best candidates in these. 
 - Go is very stable in between its version releases. That is, the language specifications aren’t changed much as new versions are released. This makes go a suitable choice for futuristic technology like blockchain.
Above all, Google designed Go. They specially made it for their highly scalable systems. Which means the language is capable of handling such systems, and it became an apt choice for handling the Blockchain platforms

A sample chaincode in go
package main
import "fmt"
import "github.com/hyperledger/fabric/core/chaincode/shim"
type SampleChaincode struct {
}
func (t *SampleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
  return nil, nil
}
func (t *SampleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
  return nil, nil
}
func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
  return nil, nil
}
func main() {
  err := shim.Start(new(SampleChaincode))
  if err != nil {
      fmt.Println("Could not start SampleChaincode")
  } else {
      fmt.Println("SampleChaincode successfully started")
  }
}
How to install Go in your system.
 - Run the given code in a terminal
sudo tar -C /usr/local -xzf go1.9.2.linux-amd64.tar.gz
Note: Replace the version number in above command with most recent version number
 - Set the environment variable
export PATH=$PATH:/usr/local/go/bin
 - Type ‘go’ in terminal
        If the installation was successful, it will show a list of options
 - To run programs, type,
       	go run <program name>.go
Eg:  go run hello_world.go



Comments

0

Leave a comment