Home > Article > Backend Development > Reasons to give up Python and choose Go in 2021
In the past period of time, the Go language Chinese website has promoted a lot of Python-related public accounts. Many people are surprised: Why are all the Python accounts promoted? In fact, the main reason is that there are too many public accounts in the Python category, and many of them have very many fans. I am also quite puzzled. But they are worth learning from, and they also show that Go still has a long way to go.
Which would you choose between Python and Go? If you have a deeper understanding of Go (Golang), then you will definitely choose it. Otherwise Python will be selected. So let's discuss it. Python friends, here are 5 reasons to choose Go. Let us read this article together.
"Python is very powerful, especially Python 3 with its async capabilities, but Go will completely take its place in large enterprises..."
If you really If you understand the above, you can try to use the Go programming language. I think Go is a very simple programming language that can adapt to any application environment, even simpler than Javascript programming. This is probably why the Go programming language has become popular in recent years.
In this article, I will compare the following aspects of both languages. Parameters along these lines will help you decide which language is better suited for your project at hand.
1. Performance
2. Scalability
3. Practical application
4. Execution method
5. Program library
6. Code readability
The website TreeScale.com homepage and API are web applications based on certain analysis tools. Here is their tech stack:
React.js Home Frontend
Django Python Home Backend and Authentication Service
Node.js API Service
PostgreSQL database, Cassandra logging system
Custom container registry in use Rust Language
Python
Repustate is almost entirely a Python shop. They use Django to implement the API and website. Therefore it is reasonable to (currently) use Python to implement the Arabic Sentiment Engine in order to maintain code consistency. For prototyping and implementation only, Python is a good choice.
Its expression ability is very powerful, and third-party class libraries are also very good. If you are working with web services, Python is the perfect choice. However, everything slows down when you perform low-level calculations and rely heavily on hash tables (dictionary types in Python) for comparisons. We could process about two to three Arabic documents per second, but that was too slow. By comparison, their English sentiment engine can process about 500 documents per second.
As you can see from the above, the basic framework is completely based on the separation of microservices and logic (separation of front-end and back-end), and is even implemented with completely different technologies.
The main reason why they integrate API services and backends into one project is that they are similar in many places and are coded in multiple languages or technologies with a lot of duplicate code. Finally, they need to spend a lot of time personalizing Django's API services, especially when it comes to customizing JSON responses.
I think using Django is great when you develop some normal applications, but when you need higher performance and some personalized modules, using Django makes it more complicated.
Why choose Go programming?
1. Compile into a single binary file
Golang is a compiled language, and the developers of Googe have put a lot of effort into it. It uses static linking to actually combine all relevant library files and modules into a single binary file based on the operating system type and environment. This also means that if you want to compile a backend application to a Linux operating system and an X86 CPU based application, You just download the compiled binary application to the server and then the backend application works without any related files.
2. Static type system
The type system is very important for large-scale applications. Python is a great and fun language, but sometimes you'll see some weird exceptions when you try to use a variable as an integer variable when it's actually of type string.
def some_view (request): user_id = request.POST.get ('id', 0)
Such code will cause Django to crash.
And Go will compile and tell you it's a compiler error, which is where it buys time on stupid problems.
3. Optimization (Performance)
Surprisingly, Go is better than Python (version 2 or 3) in most application scenarios Faster. The results of the comparison can be seen in benchmarks, which is of course unfair and depends on the type of application and user use case.
As far as we are concerned, Go has its own multi-threading module and CPU scalability, resulting in higher performance. Whenever we need to perform some internal requests, we can use Goroutine to execute them separately, which saves ten times resources than threads in Python. With these built-in language features, we can save a lot of resources (memory and CPU).
4. Go no longer requires a web framework
This is a cool thing for a programming language. The creators and community of the Go language have many built-in tools supported by the core language, and in most cases you no longer need any third-party libraries. For example, it has built-in http, json and html templates. You can build even very complex API services without having to worry about finding third-party libraries on Github.
Of course, Go also has many libraries and frameworks for building web projects, but I recommend that you do not use third-party libraries to build web projects or API services, because in most cases, use standard Library packages will make your life easier.
5. Better IDE support and debugging
IDE support is one of the most important considerations when you are trying to change programming languages. Friendly IDEs can save an average of 80% of programming time. Go Plugin for JetBrains IDEA, also provides other support, such as (Webstorm, PHPStorm, etc.). This plugin provides any services needed in project development. The powerful JetBrains IDEA can make your development more powerful, and even better is Goland.
Choose Go or go home?
Mozilla is internally converting its vast underlying logging architecture to Go, in part due to powerful goroutines. The Go language was designed by people at Google, and support for concurrency was a top priority at the beginning of the design, rather than being added later like various other Python solutions. So we started switching from Python to Go.
Although our Go code product is not yet officially released, the results are encouraging. Now we can process a thousand documents per second, use less memory, and don't need to debug the problem we encountered in Python: ugly multiprocess/gevent/"Why Control-C can't kill processes".
Why do we love Go?
Anyone who knows even a little bit about how programming languages work (interpreted vs. compiled, dynamic vs. static) will say: "Tch, of course Go is faster". Yes, we could also rewrite everything in Java and see similar and faster improvements, but that's not why Go wins.
The code you wrote in Go seems correct. I don't know what's going on, but once the code is compiled (fast compilation speed), you get the feeling that the code just works (not only runs correctly, but is logically correct). I know that sounds unreliable, but it's true.
This is very similar to Python in terms of redundancy (or non-redundancy). It targets functions first and foremost, so functional programming will be easy to understand. Sure, Go threads and channels make your job easier, you get big performance gains from static typing, and you have better control over memory allocation, but you don't have to pay much for language expressiveness.
According to our code statistics, after rewriting the project in Go, we reduced the code by 64%.
You don't need to debug code that doesn't exist. Less code means fewer bugs!
Conclusion
Go provides us with great flexibility. A language can be used in all user scenarios and work well in all user scenarios. In the backend and API services, we achieved a 30% performance optimization. Now I can process logs in real time, convert to database, and handle one or more services via WebSocket! This is a very powerful feature provided by the Go language.
For more go language knowledge, please pay attention to the go language tutorial column on the php Chinese website.
The above is the detailed content of Reasons to give up Python and choose Go in 2021. For more information, please follow other related articles on the PHP Chinese website!