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”.
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?
- DevOps – Kubernetes, 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.
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.
Let’s start by writing a simple “Hello, World!” program in Go.
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:
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.
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.
- 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.
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.
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.
We can see that there are three ways to declare arrays in Go:
- We could initialize the size of the array first and then add values one by one.
- We could also specify the size of the array and initialize all values in one go.
- 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:
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.
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:
- One is the value corresponding to the key.
- The other is a boolean which evaluates to true if the key exists within the map and false otherwise.
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.
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.
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!
- A tour of Go
- Golang Wiki
- The Go Programming Language
- Go in Action
- Go by Example
- Build Web Application with Golang
- Learn How To Code: Google’s Go (Golang) Programming Language
Join Our Telegram Group. Be part of an engaging online community. Join Here.
Subscribe to our NewsletterGet the latest updates and relevant offers by sharing your email.
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.