Bitfield Consulting

View Original

Go vs Python

I reject your false dilemma, programming language advocates

Which is better, Python or Go? Which language should you learn today, and why? How do the two compare in performance, ease of learning, scalability, and rapid prototyping? Let's find out, in this friendly and accessible overview of Python and Go for beginners, by the author of the For the Love of Go book series.

Python and Go are both awesome

First, it's really important to say that both Go and Python are absolutely excellent programming languages. They're well-designed, powerful, modern, relatively easy to learn, and ideal for almost all applications. Both have great communities, wide adoption, thriving ecosystems, and high commercial value. Whichever one you like the best is the best one to learn. End of discussion, you might think, but let's say just a little more.

It's very common to read blog posts like 'Why Python sucks', or 'Why we switched from Go to Python', and so on. These are, to put it politely, not especially valuable in understanding the characteristics of Python and Go that might affect your choice, and they generally want to argue that one language or the other is better.

But that really makes no sense. Better for what? Any programming language naturally represents a set of trade-offs: speed against safety, let's say, or ease of learning against correctness. Different languages make different trade-offs. You can safely assume that any widely-used language like Go or Python is, on average, as good as any other... but perhaps at different things.

So let's throw out this false dilemma that the choice is either Go or Python. We can learn and use both languages; both excel in different areas, and both are extremely valuable skills from a career point of view. However, assuming that you have a limited amount of study and practice time, it'll be helpful to know which language is favoured for the area of development you want to work in.

So let's look at some different areas of programming and see which language is a better fit for each.

Learning

If you're new to coding, you may be looking for different things from your programming language than someone who, say, wants to write operating systems for deep-space probes. For example, you might want a small language which doesn't have a whole lot of syntax to learn, or a flexible language which lets you express yourself in whatever way feels most natural.

Go is a deliberately small language; it doesn't have many different ways of doing the same thing, for example, and there aren't many facilities built into the language to cover every possible thing you might want to do. By comparison, Python has a lot of features: classes, lambdas, tuples, iterators, generators, and many more.

However, Python syntax feels quite natural; it looks like this:

for path in paths:
    if path == '-':
        text = sys.stdin.read()
    else:
        fp = codecs.open(path, 'r', opts.encoding)
        text = fp.read()
        fp.close()

Go is simple, but simple isn't the same as easy. If you're new to it, it can be hard to figure out what a given piece of Go code is doing:

func (forceApi *ForceApi) getApiSObjects() error {
    uri := forceApi.apiResources[sObjectsKey]

    list := &SObjectApiResponse{}
    err := forceApi.Get(uri, nil, list)
    if err != nil {
        return err
    }

On the other hand, while it may be more challenging at first, you'll rapidly get to a point where you're familiar with everything in Go. Reaching this stage will take longer with Python, simply because there's more to learn.

However, if you're taking your first steps in programming, it's likely to be an easier and more pleasant experience with Python.

High performance

If you're new to programming, you may be surprised to learn that, for most programs, performance hardly matters. Readabilty and maintainability are far more important than raw speed, in general. But when performance does matter, in applications such as games, databases, or high-scale web services, it really matters.

Go is a compiled language, which means that Go source code is translated into machine language for the specific processor you want to run it on: an ARM chip, an X86_64, or whatever.

This makes Go programs in general much faster than the equivalent programs in Python, which is an interpreted language, meaning that it's not executed directly on the CPU. Instead, another program, called the interpreter, reads Python source code and translates it on-the-fly into machine instructions.

With the caveat that performance will be far less important than simplicity and readability for 99% of the programs you ever write, if you should happen to need to write programs that execute very fast, Go will be your first choice.

Science

Scientists, including data scientists, machine learning developers, and people who need to do complex numerical and statistical analysis, love Python. It's been around a long time, and it's built up a great ecosystem for scientific computing: maths, stats, AI, simulation, image processing, distributed processing, and so on. These libraries and communities are very mature, and provide lots of resources.

It's not that you can't do these things in Go, but people have just been doing them a lot longer in Python, and it more or less owns this space. If you're working on science and engineering applications, you'll almost certainly be doing it in Python.

High scale

When we talk about 'scale', we generally mean lots of things happening very fast: millions of transactions per second, thousands of servers, terabytes of data. That creates some challenges for programmers: they need their programs to be fast, small, easy to deploy and operate, and ideally to have great support for concurrency.

These are all areas where Go is very strong. As we've seen, Go programs are compiled, so they naturally execute quickly, and they can be deployed as a single small binary file. By comparison, Python programs are deployed in the form of source code, and they also need a Python interpreter, libraries, and so on. Even using containers, it's much easier to deploy and run applications at scale with Go.

Go was also designed for concurrency from the start. It supports super-lightweight threads, called goroutines, and a single program can run millions of goroutines concurrently without causing problems. While Python supports concurrent processes and threads, it's a little more complicated to use than Go, and the performance isn't as good.

For high-scale, low-latency applications, especially involving a lot of concurrency, Go is the first language you should look at.

Rapid prototyping

It's very common in software development that we don't have all the detailed requirements for the program in advance. Instead, we just want to get something up and running that proves the concept, and gives us a simple prototype we can show to users, so we can get feedback.

Python is an excellent language for rapid prototyping; we can get our thoughts down quickly in a form that almost looks like English, and the resulting program will probably run, more or less. Python's dynamic typing is very forgiving; it means you don't have to know exactly what something is in order to deal with it.

By contrast, Go is rather strict. It's statically typed, which means the compiler insists on verifying beforehand that every value you pass to a function, say, is exactly the type it's supposed to be. This can make the programming experience feel a bit like doing a lot of paperwork, just to get a simple proof of concept running.

For super-fast prototype development, experimentation, and solving one-off problems with a quick script, Python is ideal.

Conclusion

I hope this article has convinced you that both Python and Go deserve your serious consideration. If at all possible, you should aim to get at least some level of experience in both languages, because it will be incredibly useful to you in any tech career, or even if you enjoy programming as a hobby.

You know now enough to reject all the clickbait articles aiming to convince you that either Go or Python is better than the other in some absolute way, or at least to read them with a very critical eye, and understand that they largely just express the authors' personal preferences.

That's okay: programming is a highly subjective and personal activity, and you'll be far more productive in the language you like best than the one which a panel of computer scientists rate as theoretically superior. Also, you won't feel the need to write blog posts insulting people who prefer a different language; you understand that they just prioritise different things.

So it really makes sense for you to try both Python and Go, and see which you enjoy programming in more, doesn't it? When you've written a bunch of toy programs or coding challenges, and perhaps one non-trivial program in each language, you'll be in a much better position to decide which one you want to prioritise learning more about.

One final point: a good programmer is a good programmer, whatever language they happen to be working in at the moment. Most of the skills you learn as a software developer will translate very well to other languages and tech stacks, so the choice of what to study today isn't critical.

Ignore the hype, skip the over-zealous blog posts, and make your own mind up: when it comes to the choice between Python and Go, only your informed opinion matters.

Going further

If you found this article useful, you might enjoy my introductory series of ebooks, For the Love of Go, or my other Go tutorials on this site. If you'd like to learn more about Go, then I can help! I offer one-to-one or group training in Go development, for complete beginners through to experienced Gophers. Check out my Learn Golang with mentoring page to find out more, or email go@bitfieldconsulting.com. Looking forward to hearing from you!

Gopher image by egonelbre