Golang vs. Python: Which One to Choose?

Author: Paweł Dziubałka and Sebastian Karasiewicz
7 min read
Golang vs. Python: Which One to Choose?

The internet is filled with great codes. They form the bedrock on which various infrastructures are built. Even the very platform you are reading this on is built with a dedication to creating great codes. While the average user isn’t going to take note of this, good developers, on the other hand, are always seeking to make their applications (and hence, their coding) better.

The foundation on which great code is written rests on the skill of the developer and his/her language of choice. This inevitably leads to some epic clash between developers not just on skill level, but mostly on their preferred language. This is the result you are most likely to obtain when you place two great programming languages such as Golang(Go) and Python side by side.

However, let us step back a bit and evaluate both languages from the point of merit and functionality. Let’s take away personal bias and preferences. Then level the playing field, skill-wise. Now, all we are left with are two great choices: Go vs Python. The question now becomes, which is right for you?

A Python’s tale

Ask some developers and you will hear that there was nothing before Python and there will be nothing after it. Over the years, it has managed to gain a cult-like following because it is a very good programming language. The internet is filled with wonders created using Python.

Python is old, in terms of programming years. It was first conceptualized back in 1991. And with age comes certain advantages. It has a wide following which translates to it being stable and well documented. In most cases, you will find libraries for almost everything and code samples for just about everything you can think of. What this means for devs and businesses is that the choice of using Python comes with it a wealth of experience and coding just waiting to be accessed.

There are plenty of open-source projects that use Python as a base, so in most cases, you are not starting from scratch. It is well integrated into enterprise applications and can be used in machine language and AI applications. But it does have its downsides. For one, it is not ideal for memory-intensive tasks, a bit on the slow side for executions and unsuitable for mobile application development.

Speaking of Golang(Go)

Developed at Google back in 2009. Go was a solution to a problem. Its aim was to create a language that took away all the baggage and excesses found in languages such as C++. This gives it a performance and speed boost that makes working with it a delight. Plus, most developers picking up Go for the first time are not going to be left adrift as the familiar elements and ease of use would come as a pleasant surprise.

This is not to say the language is perfect in all cases. While it takes speed and elegance to the next level, it does, however, leave behind some a few things to be desired. For one, it does not have an extensive library, nor support for inheritance. In addition, there is no GUI library or object-oriented programming support. What it does have going for it is a lightweight thread (Goroutines), smart standard library, strong built-in security and is easy to code with minimal syntax.

The test of performance

We can talk endlessly about the pros and cons of Golang or Python but when it’s down to the wire, what we all look for it how they perform. So we did some testing on our test rig running an Intel® Core™ i7-9700K CPU @ 3.60GHz × 8 cores. And the code used is available here.

We created lists of ints (from 1 to 100000) and then used binary search to find always the same number 729. The results are:

LanguageSpeed
Go20.8 ns/op
Python2442.13377 ns/op

2. Bubble sort

Again we generated a list of integers (this time random ones, 10000 elements long) and sorted those using bubble sort algorithms.

LanguageSpeed
Go90805247 ns/op (0,09s)
Python6708160950.6 ns/op (6,7s)

3. Read from file

A simple test of reading “lorem ipsum” text file.

LanguageSpeed
Go5305 ns/op
Python58359 ns/op

4. HTTP Request handling

Making an HTTP server is really easy, but we made an effort and did the analog one in Python. We checked how long would it take to respond with a simple “hello world!” message.

LanguageSpeed
Go0.070 ms/RQ
Python1.261 ms/RQ

Which to choose?

While Python has remained a community favorite retaining the #2 spot in the first quarter of 2019 for the fastest programming language on GitHub in terms of pull requests (+17%), Golang isn’t so far behind and is hot on its heels at #4 (+8%). The choice between Golang vs Python becomes even more blurry. Regardless, there are a few things to be considered when selecting which might be right for you.

  1. Scalability: Golang was created with scalability in mind. It comes with inbuilt concurrency to handle multiple tasks at the same time. Python uses concurrency but it is not inbuilt; it implements parallelism through threads. This means that if you are going to work with large data sets, then Golang would seem to be a more suitable choice.

  2. Performance: Python is not known to be memory or CPU friendly but with its huge number of libraries, Python performs efficiently for basic development tasks. Golang comes with build-in features and it is more suitable for microservices software architectures.

  3. Applications: Python shines when used to write codes for artificial intelligence, data analytics, deep learning, and web development. Whereas Golang has been used for system programming, it is loved by developers who use it for cloud computing and cluster computing applications.

  4. Community & Library: As mentioned earlier, Python’s age gives it certain advantages. One of which is the number of libraries it has and the large community that supports it. Golang, on the other hand, is still a growing language and does not have the number of libraries and community support that Python commands. Yet we should not count Go out just yet. Its rate of growth and adoption is incredible and it is expanding every day.

  5. Execution: If speed is the name of the game, then Golang wins by a mile.

After taking all these into account, your use case will be the determining factor in which language to adopt. Given a scenario where you are setting up a development team to create microservices, Golang would be the more reasonable choice here as its both fast, easy to code with and can scale excellently well. Python, on the other hand, is more geared towards AI, ML and data analysis.

So going head to head, Go would come out on top in most cases and is considered to be a valid alternative to using Python. Developers need to choose a programming language considering their nature and size of the development project as well as the skill set of those involved.

The good news, however, is that regardless of choice, both languages are ever-evolving. While Golang might seem like an obvious choice in most cases, the Python community isn’t just sitting back and doing nothing. Both languages are expanding and growing. This means that we will be seeing more functionality and improvement in the future.