GoLang VS Python

We know that Python is fast when it comes to general scripting use cases, and I’ve always been an ardent user of Python, from version 2 to 3 and its painful migration. Only recently, have I decided to pick up another language, GoLang, and was surprised to see that it was way faster. Although GoLang sacrifices a little on the flexibility being statically typed, and the absence of certain inbuilt structures and functions, it is also because of those that the language is about 40 times faster than Python.

In this post, i’ll talk about the differences, and as I go along, I’ll also add in why they make GoLang much faster.

Explicit Differences

Python is dynamically typed, while GoLang is statically typed. This means that in Python, when you instantiate a variable, you can reassign it to other data types down the flow. In GoLang, once a variable has been assigned a specific data type, it cannot change, and can only be assigned variables of that type.

Python is interpreted, while GoLang is compiled. Understanding the difference here requires understanding the difference between machine code and byte code. When a program is compiled, it is translated to machine code, where it can be executed directly on the machine. When a program is interpreted, it is compiled down to byte code, and the byte code is then executed by an interpreter, which is essentially a virtual machine. In the case of Python, the interpreter is typically installed at /usr/local/bin/python3.x. The interpreter then has to turn that byte code into machine code to be executed on the machine. It is this reason that Python is slower than GoLang, but of course being interpreted has its own set of benefits.

GoLang supports concurrency, and in fact was one of the principles built into it when the language was created. This was because the language was made as being a server side language to begin with, and that functionality naturally required the feature of high concurrency. Python on the other hand does not have any in-built concurrency mechanisms.

The winning point for Python right now is its large support for various tools and libraries, especially in the domain of data science and data analytics. GoLang has very little of such libraries due to the it being more backend by design, and with that, the libraries available in Go are mostly server side, such as database handling and web development.

All in all, I think in future applications, when it comes to the separation of frontend and backend, GoLang can be a very viable choice for server side implementation due to its better performance over Python. For all the other interactive, analytical, and visual stuff, Python would perhaps still be the better option.

Implicit Differences

There is also an implicit difference that I personally find between Python and GoLang, which is limited creativity, in a good way.

In GoLang, you can’t be as creative to perform certain tasks as compared to Python. For instance, there are many ways you could craft your code, such as

  • Use MetaClasses to self-register classes upon code initialization
  • Swap out True and False
  • Add functions to the list of built-in functions
  • Overload operators via magic methods
  • Use functions as properties via the @property decorator

In GoLang however, because of its deliberate simplicity, it forces the developers to stick to the very basics and not get too creative in doing things wildly different. Being too creative and allowing too much expression might cause code readability issues, where other developer have a hard time understanding the code written, when the logic is exactly the same.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s