Golang vs. Python — Which One to Choose?

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.

Golang vs Python 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

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)

Read from file

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

LanguageSpeed
Go5305 ns/op
Python58359 ns/op

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

Python vs Golang web frameworks

One of the biggest advantages of using Python is the vast collection of libraries and frameworks that are available. Django and Flask are two of the most popular Python web frameworks which enable you to build a web application or API in the fastest time possible.

Golang doesn’t have a single dominant framework like Django for Python or Laravel for PHP and Go community is in a way averse to frameworks. Go 2018 Survey Results show Golang web development is the most prevalent at 65%, and it increased its dominance as the primary area survey respondents work in (up from 61% last year): web development has been the most common domain for Go development since 2016.

Here the most popular Golang web frameworks:

Gin

Gin is a Golang web framework that is known for developing high-performing APIs. It is also one of the simplest frameworks to learn. It takes around 10 minutes to get used to it. If you need performance and good productivity, you will love Gin.

Features

  • Faster performance: Radix tree based routing, small memory foot print
  • Middleware support: An incoming HTTP request can be handled by a chain of middlewares and the final action
  • Routes grouping: Organize your routes better
  • Error management: Gin provides a convenient way to collect all the errors occurred during a HTTP request
  • Rendering built-in: Gin provides a easy to use API for JSON, XML and HTML rendering
  • Extendable: Creating a new middleware is so easy, just check out the sample codes

Echo

Echo is a high performance, extensible, and minimalist Go web framework providing optimized HTTP router, group APIs, data binding for JSON and XML, HTTP/2 support, and much more.

While Gin is a little more popular (36k GitHub stars), many developers choose the Echo (16k GitHub stars) because it has better documentation and is a little faster framework.

Features

  • Optimized HTTP router which smartly prioritize routes
  • Robust and scalable RESTful API, easily organized into groups
  • Extensible middleware framework, many built-in middlewares, middleware can be set at root, group or route level
  • Data binding for JSON, XML and form payload
  • Handy functions to send a variety of HTTP responses, including JSON, XML, HTML, File, Attachment, Inline, Stream or Blob
  • Customized central HTTP error handling and easily extendable API

Go kit

Go kit is a programming toolkit for building microservices (or elegant monoliths). Go Kit is an opinionated framework. It solves common problems in distributed systems and application architecture so you can focus on delivering business value.

Features

  • Operate in a heterogeneous SOA — expect to interact with mostly non-Go-kit services
  • RPC as the primary messaging pattern
  • Pluggable serialization and transport — not just JSON over HTTP
  • Operate within existing infrastructures — no mandates for specific tools or technologies

Companies using Golang

Golang is widely adopted by top companies. There are over one million developers using Go worldwide — spanning banking and commerce, gaming and media, technology, and other industries, at enterprises as diverse as PayPal, Capital One, IBM, Salesforce, Twitch, Uber, Dropbox, and of course Google.

Here are some sample case studies:

### PayPal Taps Golang to Modernize and Scale Go’s value in producing clean, efficient code that readily scales as software deployment scales made the language a strong fit to support PayPal’s goals. View case study →
### American Express Uses Golang for Payments & Rewards Go provides American Express with the speed and scalability it needs for both its payment and rewards networks. View case study →
### Salesforce ditched Python for Golang in Einstein Analytics Salesforce explains why Google's Go programming language is a better fit than Python and C for Einstein Analytics. View case study →
### Stream - Why we switched from Python to Go We have a small development team at Stream powering feeds and chat for over 500 million end users. Go’s combination of a great ecosystem, easy onboarding for new developers, fast performance, solid support for concurrency and a productive programming environment make it a great choice. View case study →
### Pydio rewrote enterprise-ready filesharing in Golang Pydio, cloud storage solution from before Dropbox, turned a full-fledge Sync-and-Share platform and the application stuck to its initial technical stack: LAMP, as in Linux/Apache/MySQL/PHP. Read an article about motivations of rewriting entirely in Golang, the new architecture and the choices behind it. View case study →
### Repustate migrated API from Python (first Django then Falcon) to Go The migration reduced the number of EC2 instances required by 85% and the the mean response time of an API call from 100ms to 10ms. Due to the similarity between Python and Go, they were able to quickly re-purpose our unit tests written in nose to fit the structure that Go requires with a just a few simple sed scripts. View case study →

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.