MITB Banner

Introduction To Golang For Python developers

Share

Golang or Go is one of the fastest-growing programming languages today. The popular “C-like” language created by tech giant Google is quickly becoming one of the mainstream languages for developers. 

A survey by HackerEarth showed that out of 16,655 developers, over 32% of working professionals cited Go as “the most sought-after programming language”.

Source: zdnet.com

Because Go is still a very new language, the demand for Golang developers severely outweighs the supply.

Why has Go become so popular?

  • Easy to understand: Much of Golang’s popularity is drawn to its simplicity. The syntax is simple (like Python) and was explicitly designed to be very clean and accessible.
  • Standing on the shoulders of giants: The creators of Go:  Robert Griesemer, Rob Pike, and Ken Thompson are pioneers of modern Computer Science. Ken Thompson was also part of the original team of developers responsible for creating the Unix operating system. He also created the B programming language (predecessor to C).
  • Modern capabilities: Golang was created in an era where computing had largely become distributed. Its native support for concurrency, parallelism, compatibility with the cloud, scalability, etc. makes it an asset for new-age technology.
  • Performance: Unlike Python, Go is compiled to machine code. This makes it blazing fast! Here is a benchmarking comparison between Python and Go. In all of the tests, Go absolutely blows Python out of the water.

Where is Go being used?

  • DevOpsKubernetes, and Docker are written in Go.
  • Distributed services – Because of native concurrency, Go is well suited for creating APIs, web servers, microservices.
  • General Backend infrastructure – Go’s speed, simplicity, and memory safety has made it a popular choice as a backend language. Subsequently, many organizations are rewriting their existing infrastructure in Go.
  • Tooling – Just like Python and Javascript, Go’s requirements of smaller dependencies makes it a great language to write tools and utilities.

Future use cases:

  • Blockchain – While blockchain technology is itself in a nascent stage, Go is quickly becoming a popular choice for the domain. Ethereum has dedicated an entire developer documentation site to develop decentralized applications (or “dapps”) using Go.
  • Data Science/Data Engineering – Python is still the language for Data Science. However, I do see Go making some inroads into this field in the future. For example, Apache Beam does have an SDK for Go.

Hello, World!

Let’s start by writing a simple “Hello, World!” program in Go.

Source: carbon.now.sh

As you can see, the syntax is very simple and clean. One thing to note is that unlike Python, we must include the code within the main function. Otherwise, our code will throw an error. 

Here is the equivalent in Python:

Source: carbon.now.sh

Declaring Variables

There are two ways of declaring variables in Golang. One is where the type is explicitly defined. The other is where we “infer” the type. 

For numerical values, we infer the type based on precision.

Source: carbon.now.sh

Notice that when we explicitly define the type, we use the keyword “var” followed by the variable name followed by its type and then we assign it to some value using “=”.

When we infer the type, we simply assign the value to a variable name using the “:=” operator. 

This operator tells Go that we wish to infer the type from the value. 

In Go, variables that are in the scope of a function have to be used. Otherwise, the program will not compile. 

Note that global variables are not required to be used in order for the program to commpile.

Like variables declared with function scopes, unused imports in Go will also throw errors. 

In comparison, variable declarations in Python are very straightforward because of Python’s dynamic typing system.

Source: carbon.now.sh

Data Types

  • Integers: There are four distinct sizes of integers (both signed and unsigned) – 8, 16, 32, 64 bits whose corresponding types are represented by int8,int16,int32, and int64 respectively. Their unsigned counterparts are uint8, uint16, uint32, and uint64 respectively. There are also types called int and uint respectively which provide a more natural way to represent integers. These will pick the most efficient size.
  • Float: Floating point numbers in Go have two sizes float32 and float64. Unlike integers, the float size has to be mentioned when declaring the variable.
  • String: Go strings are defined using the keyword string. As you know, strings are useful for storing human-readable text.
  • Boolean: As with other programming languages, booleans or bool in Go have only two values true or false. They are used to evaluate the truth value of statements.
  • Constants: Constants in Go can be declared by using the keyword const. They are declared similarly to variables. For example, const Pi = 3.14

There are other data types available in Go that we will not look into such as complex64 and complex128 for complex numbers. 

To learn about these you can read The Go Programming Language Specification on Variables.

Composite Types

The composite types in Go are made of other types like built-ins and user-defined types. Go does have several composite types. We will look at a few of these.

Arrays:

Much like lists in Python, arrays in Golang are used to store a collection of data types. 

In arrays, elements are stored in a sequential manner. Therefore, searching for elements within an array takes O(n) time complexity.

Python allows storage of multiple data types within it’s list data structure.

Go arrays only allow the storage of only one data type. 

Additionally, unlike Python lists, the size of Go arrays have to be fixed.

Source: carbon.now.sh

We can see that there are three ways to declare arrays in Go:

  1. We could initialize the size of the array first and then add values one by one. 
  2. We could also specify the size of the array and initialize all values in one go. 
  3. Lastly, we could choose to let the compiler infer the size of the array and initialize values directly.

We can perform numerous useful operations on arrays. Here are some of them:

Source: carbon.now.sh

Slices:

The concept of slices is a very interesting addition to Go. Slices are thought to be “views”, “references” or “windows” on top of an array.

 Slices can also hold elements of the same data type. However,  they are “dynamic” i.e, their size can vary. 

We can create slices in a very similar fashion to arrays. Slices can also be created out of existing arrays. 

Like Python lists, we can append values to slices in Go using the slice() function.

Source: carbon.now.sh

Maps:

Map is the implementation of hash tables in Golang. The equivalent of maps in Python is dictionaries. 

Maps store data in the form of key value pairs. Each key is mapped to a correspondingvalue. 

Time taken to search for elements within a hash table is very fast (O(1)) time. Data within traditional hash tables are not stored in any particular order.

We can iterate over key value pairs using the range keyword. 

To delete items, we use the delete() function within Go.

When we access elements in Go maps, there are two values returned:

  1.  One is the value corresponding to the key.
  2. The other is a boolean which evaluates to true if the key exists within the map and false otherwise.
Source: carbon.now.sh

Structs:

A struct (short for structure) is a collection of fields in Go. 

In Golang, we can create our own data types by combining different data types. Structs are concrete user-defined data types.

We can group different data types using structs to form records.

Struct fields can be accessed with the “.” operator. Structs are also mutable.

After creating a struct, we can creates instances of it which can be used to store records of data.

Source: carbon.now.sh

Functions

The last thing we will talk about in this article are functions. 

Because of the absence of Classes, functions play a crucial role in structuring logic in Go.

A function is a piece of code that can be reused to perform a single action or task. For example, we have already used the built-in delete() function to delete items within a map.

Similar to the def  keyword in Python, go uses the func keyword to create user defined functions. 

A function is always expected to return something.

Lets create a simple function to calculate the average of an array of numbers.

Source: carbon.now.sh

In the above example, we create a slice of 5 integers.

Within the average() function we iterate over each element and compute the summation. 

The sum is then divided by the length of the slice and then converted to type float64 to return the average.

I hope you enjoyed this small introduction to Golang. Everything written in this article is just a drop in the ocean. If you want to become a Go developer, I will attach a few resources/books that you can use to learn Go. Until next time!


Resources:

  1. A tour of Go
  2. Golang Wiki
  3. The Go Programming Language
  4. Go in Action
  5. Go by Example
  6. Build Web Application with Golang
  7. Learn How To Code: Google’s Go (Golang) Programming Language
Share
Picture of Sayar Banerjee

Sayar Banerjee

Hi, my name is Sayar Banerjee and I work as a Data Scientist in a startup called Indihood based out of Bangalore India. I am a Kaggle expert and also work as a part-time machine learning consultant for a stealth mode fashion tech startup. In my spare time, I like to write articles about the field of Data Science. I am a writer for Towards Data Science and Towards AI publications which are two of the finest Data Science publications on Medium. I graduated with a Bachelor's degree, double majoring in Economics and Statistics from The University of Illinois At Urbana-Champaign.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.