科技和互联网

为什么从Python转到Go

Python很酷,很现代,是互联网Web和App的绝佳的工具,也是近年来编程语言的热门,甚至可以说是最热门。它可以非常快速地完成许多互联网任务,而用传统的C,C++,可能学习时间都会是一个痛苦而漫长的过程,更不用说马上开发网络应用了。现在的互联网应用基本都要涉及几个流行的平台或框架,前端和后端往往可以交叉混用。比如前端用React.js,后端是Django,用Node.js提供API服务,再选一个合适的SQL数据库,这些都是基本需要的部件,而且Django都可以应付,唯一的缺点是,当复杂度随着功能模块增加而增加得很快,性能不尽如人意。

相比Django,Go也是非常现代的语言,而且也非常容易学习。同时,在几个方面有很大的优势,首先Go可以把固定链接库编译进一个单独的二进制文件,其次是不需要其他的Web框架,因为Go原生支持基本Web框架,最重要但不是最后的是它的性能超出Python很多,有些任务的表现可以媲美C语言。而且,作者实现从Python转到Go以后,所有项目的代码总数少了64%,少代码,少Bug!

原文链接:https://hackernoon.com/

抄录:

5 Reasons Why We switched from Python To Go

By Tigran Bayburtsyan

Gophers from golang.org
Python is awesome! Especially Python 3 with async functionality. But really Go is not giving any chances to survive in enterprise world…
If you got the idea of this quote, then you probably tried or just looked around Go programming language. Which is I think easiest programming language that can fit in any kind of application development. Yes! you read that right, for me Go is easier to learn than even JavaScript. I think this is the main reason why Go language become so match popular within a few years.

So What We Had Before Using Go?
TreeScale.com mainly dashboard and API based web application with some analytical stuff. Here is our technical stack

React.js for Dashboard Frontend
Django Python for Dashboard Backend + Authentication service
Node.js for API Service
PostgreSQL for Database and Cassandra for Logging
Custom written Container Registry with Rust Language
As you can see the overall infrastructure is fully micro-service based and most of the logic completely separated, even with different technologies.

That’s the main reason why we started thinking about merging API Service and Backend into single project because they have a lot of things that are similar but codebase is duplicated over multiple programming languages and technologies. But it turns out that we need to work a lot to customize Django’s API services, especially with custom JSON responses.

After 1 month of building API service with Django, I felt that Django is great until you building something very usual, but when you want more performance and some custom modules things getting more and more complicated.

Why Go?
As I told earlier Go Language is the most easiest languages that I’ve ever seen and used. But it was only the first attraction for me.

Here is the main 5 reasons why we choose Go over Python Django

#1 It Compiles Into Single Binary

Golang built as a compiled language and Google developers did great job with it. Using static linking it actually combining all dependency libraries and modules into one single binary file based on OS type and architecture. Which means if you are compiling your backend application on your laptop with Linux X86 CPU you can just upload compiled binary into server and it will work, without installing any dependencies there!

#2 Static Type System

Type system is really important for large scale applications. Python is great and fun language but sometimes you are just getting unusual exceptions because trying to use variable as an integer but it turning out that it’s a string.

Django will crash process because of this

def some_view(request):
user_id = request.POST.get(‘id’, 0)

If this post request has “id” parameter then

user_id would be a string,

but you really thinking it is integer

User.objects.get(id=user_id)
Go will let you know about this issue during compile time as a compiler error. This is where you winning time for this kind of stupid issues.

#3 Performance!!

This could be surprising but in most of the application cases Go is faster than Python (2 and 3). Here is the results of Benchmarking Game, which are unfair, it depends on application type and usecase.

For our case Go performed better because of his concurrency model and CPU scalability. Whenever we need to process some internal request we are doing it with separate Goroutine, which are 10x cheaper in resources than Python Threads. So we saved a lot of resources (Memory, CPU) because of the built in language features.

#4 You Don’t Need Web Framework For Go

This is the most awesome thing about programming language. Go language creators and the community have built in so many tools natively supported by language core, that in most of the cases you really don’t need any 3rd party library. For example it has http, json, html templating built in language natively and you can build very complex API services without even thinking about finding library on Github!

But of course there is a lot of libraries and frameworks built for Go and making web applications with Go, but I’ll recommend build your web application or API service without any 3rd party library, because in most cases they are not making your life easier than using native packages.

#5 Great IDE support and debugging

IDE support is one of the most important things when you are trying to switch your programming language. Comfortable IDE in average can save up to 80% of your coding time. I found Go Plugin For JetBrains IDEA which has support also for (Webstorm, PHPStorm, etc…). That plugin is giving everything that you need for project development with the power of JetBrains IDEA you can really boost your development.

Based on our code base statistics, after rewriting all projects to Go we got 64% less code than we had earlier.

You really don’t need to debug code which doesn’t exists. Less code, less troubles!
Conclusion
Go gave us huge flexibility, single language for all usecases and for all of them it working very very well. We got about 30% more performance on our Backend and API services. And now we can handle logging real time, transfer it to database and make a streaming with Websocket from single or multiple services! This is awesome outcome from Go language features.

Don’t forget to “Recommend” this post and share your thoughts about this.

 

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

READ MORE ABOUT: Programming Python Golang

Tagged , , ,

我忍不住要留言