Programming With Go (GOlang) And It’s Benefits

GOlang

“Go will be the server language of the future.” — Tobias Lütke, Shopify

Nothing makes a developer go crazy than digging more for a new programming language. So, I started learning GOlang. Though it is not a much popular programming language like others languages (C, C++, Java, Python, C# etc), but its features and advantages are surely gonna make you incline towards its usage!

Here, I am going to explain what is Golang and the current stage of computer hardware-software and why we need new language like Go?

Let’s go ahead…

Introduction To GO

Go is a programming language introduced by Google in 2009 developed by Robert Griesemer, Rob Pike and Ken Thompson.

It is also known as Golang, Developed in traditional ALGOL (short for Algorithmic Language) and C.

It has features like Garbage Collection, Memory Safety, Statically Typing and CSP -Style Concurrent Programming (Communicating Sequential Processes).

The compiler and other language tools are open-source, originally developed by Google.

It contains two major implementations:

  1. Google’s Go compiler (gc) supports different platforms like Linux, Windows, OS X, BSD (Berkeley Software Distribution, Unix operating system) and Unix versions. It also supports Mobile devices including smartphones.
  2. The second one is ‘gccgo’ compiler, which is a GCC (GNU Compiler Collection) front-end.

It is not required any integrated development environment, but supporting them well and also Supporting networking and multiprocessing.

Language Design

Go language is similar to traditional C language, but make many changes to improve simplicity, safety and so on.

Some Improvement features are as follows:

  1. It reduces compilation time.
  2. Variable declaration and initialization through type inference (the automatic detection of the data type of an expression in a programming language).
  3. Remote package management.
  4. Built-in concurrency primitives: light-weight processes (goroutines), channels, and select statement.

Language Tools

Go includes the same process of debugging, testing, and code-vetting tools as other language distributions but the Go distribution also includes :

  • build – go build, which builds Go binaries using only information in the source files themselves, no separate make files.
  • test – go test, for unit testing and micro benchmarks.
  • fmt – go fmt, for formatting code.
  • get – go get, for retrieving and installing remote packages.
  • vet – go vet, a static analyzer looking for potential errors in code.
  • run – go run, a shortcut for building and executing code.
  • doc – godoc, for displaying documentation or serving it via HTTP.
  • rename – gorename, for renaming variables, functions, and so on in a type-safe way.
  • generate – go generate, a standard way to invoke code generators.

Example:

Types

Go also has a number of types of other programming languages.

1. Numeric

  • Integer
  • Floating point Values

2. Boolean

  • True
  • False

3. Character String

4. Derived

  • Pointer
  • Array
  • Structure
  • Union
  • Function
  • Slice – It allows to increase/decrease the size of an array dynamically.
  • Interface – Replace class inheritance, provide Run time polymorphism.
  • Map – It maps unique keys to values, the key is an object which is used to retrieve values at a later date.
  • Channel – Channels are the pipes that connect concurrent Goroutines, we can send values into channels from one Goroutine to another Goroutine.
  • Package

Hardware Limitations

First Pentium 4 processor with 3.0 GHz clock speed was introduced in 2004 by Intel. You can see the comparison of increasing the processing power with the time in below chart.

40-years-processor-trend

You can see that the single-thread performance and the frequency of the processor remained steady. So for this problem, we have solutions like…

Adding more and more cores to the processor, added more cache to the processor to increase the performance and We can also introduce hyper-threading.

Nowadays we have quad-core and octa-core CPU’s available.

But these solutions also have its own limitations like adding more and more processors having its own cost.

So, if we cannot work on the hardware improvements, the only way is to go, more efficient software to increase the performance, but the modern programming language is not much efficient.

“Modern processors are alike nitro-fueled funny cars, they excel at the quarter mile. Unfortunately, modern programming languages are like Monte Carlo, they are full of twists and turns.” — David Ungar

 

GO Has Goroutines

A goroutine is a lightweight thread managed by the Go runtime.

go f(x, y, z)

starts a new goroutine running

f(x, y, z)

The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

Goroutines run in the same address space, so access to shared memory must be synchronized. The synchronizes package provides useful primitives.

Most of the modern programming languages (like Java, Python etc.) are from the ’90s single-threaded environment. Most of those programming languages support multi-threading, but the real problem comes with concurrent execution, threading-locking, race conditions and deadlocks.

For example, if we create a thread in java it takes 1 MB of memory from the heap. But Go was released in 2009 when multi-core processors were already available and keeping concurrency in mind. It has goroutines instead of threads which consume almost 2 KB memory from the heap. So, you can spin millions of goroutines at any time.

Other benefits of Goroutine :

  • Goroutines ensures a faster startup time than threads.
  • Goroutines are loaded with built-in primitives that communicate safely among themselves (channels).

Code Written In Go Is Easy To Maintain

Go have very neat and clean syntax to do programming. The designers of the Go at google had this thing in mind that code should be simple to understand for other developers and one segment of code should has minimum side effect on another segment of the code. That will make code easily maintainable and easy to modify.

Go doesn’t have many features of modern OOPs languages like…

  • It doesn’t have Classes.
  • Golang doesn’t have Constructor.
  • It Doesn’t support Inheritance.
  • Golang doesn’t have Generics.
  • Golang doesn’t have Exceptions.

Advantages of GOlang

  1. Testing Support, it provides a feature of parallel unit testing in our code.
  2. It provides the replacement of inheritance by Interface system.
  3. Golang Package system which provides the facility to use the functionality of other files, so there is no need to add any header files in the program.
  4. Go language provides built-in functionality and library support, for writing concurrent programs. Concurrency of the program in Go language refers not only to CPU parallelism but also to CPU asynchrony.
  5. It provides statically typing,  which take care of compilation, type conversion and comparability as well.
  6. It has a powerful standard library, which helps developers to develop projects in a effective and easy manner.
  7. Go has garbage collection.
  8. Go compiler provides error checking that is able to detect variables that went unused, packages that are miss or imports those packages which are necessary to run and mistyped or invalid operations.

Disadvantages Of GOlang

  1. The packages distributed with Go are pretty useful, but there are still some libraries you’ll miss. Most notably a UI toolkit.
  2. There is no support for generics in Go.
  3. Lack of language extensibility (through, for e.g, operator overloading) makes certain tasks more complex.
  4. Lack of compile-time generics leads to code duplication, meta-programming cannot be statically checked and the standard library cannot offer generic algorithms.

Conclusion

Go is efficient, scalable and productive. Some programmers find it fun to work in, others find it boring. Go was designed to address the problems faced in software development at Google, which led to a language that is not a breakthrough research language but is still an excellent tool for engineering large software projects.

Many companies use Go, some name are as follows:

  • BBC Worldwide
  • Canonical
  • Heroku
  • Nokia
  • SoundCloud

Some properties which make programming easy with Go are as follows:

  • Clear dependencies
  • Composition over inheritance
  • Clear syntax
  • Clear semantics
  • Simplicity provided by the programming model (garbage collection, concurrency)
  • Easy tooling (the go tool, gofmt, godoc, gofix)

Projects using Go (Reference Wikipedia)

Some notable open-source applications in Go include:

  1. The “Lightning Network”, a bitcoin network that allows for fast Bitcoin transactions and scalability.
  2. Caddy, an open source HTTP/2 web server with automatic HTTPS support.
  3. CockroachDB, an open source, survivable, strongly consistent, scale-out SQL database.
  4. Dgraph, an open source, scalable, distributed, highly available and fast graph database.

Hope my GO session was quite informative for you all!

2 thoughts on “Programming With Go (GOlang) And It’s Benefits”

Leave a Comment