程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Why do we switch from Python to go

編輯:Python

List of articles

      • List of articles
    • reason 1 – performance
    • reason 2 – Language expression is very important
    • reason 3 – Developer productivity and not very creative
    • reason 4 – Concurrency and channels
    • reason 5 – Fast compile time
    • reason 6 - Team building ability
    • reason 7 – A strong ecosystem
    • reason 8 – Gofmt, Force code formatting
    • reason 9 – gRPC And protocol buffer
    • shortcoming 1 – Lack of framework
    • shortcoming 2 – Error handling
    • Inferiority 3—— Package management
  • Conclusion

Edited from : https://blog.csdn.net/m0_67391870/article/details/125383425

List of articles

Link to the original text :https://softwareengineeringdaily.com/2021/03/03/why-we-switched-from-python-to-go/

Switching to a new language is always a big step , Especially if only one of your team members has experience with the language . Earlier this year , We will Stream The main programming languages are from Python Switch to Go. This article will explain why we decided to give up Python Switch to Go Some of the reasons .

reason 1 – performance

Go Very fast . Performance similar to Java or C++. For our use cases ,Go Often than Python fast 40 times . This is a Compare Go and Python Small benchmark game .

reason 2 – Language expression is very important

For many applications , Programming languages are just the glue between applications and databases . The expression of language itself is usually not important . However ,Stream One is for 700 Companies and more than 5 Billion end users feed And chat platform API provider . these years , We've been optimizing Cassandra、 PostgreSQL、 Redis wait , But in the end , We have reached the limit of our language .Python It's a great language , But for serialization / Deserialization 、 For use cases such as sorting and aggregation , Its performance is quite slow . We often encounter performance problems ,Cassandra need 1ms To retrieve data , and Python need 10ms To convert data into objects .

reason 3 – Developer productivity and not very creative

have a look How do I start Go course A short paragraph in Go Code .( It's a great tutorial , It's also learning Go A good starting point for .)


If you are Go Novice , There won't be much to surprise you when you read that little snippet of code . It shows multiple assignments 、 data structure 、 The pointer 、 Format and a built-in HTTP library . When I first started programming , I always like to use Python More advanced features .Python Allows you to be quite creative when writing code . for example , You can :

  • Use MetaClasses to self-register classes upon code initialization Use metaclass from register class during code initialization
  • Swap out True and False Replace True and False
  • Add functions to the list of built-in functions Add a function to the list of built-in functions
  • Overload operators via magic methods Overload operators by magic
  • Use functions as properties via the @property decorator adopt @property decorator Use functions as properties

These features are fun to play with , however , As most programmers would agree , When reading other people's works , They often make the code harder to understand .Go Force you to stick to the basics . This makes it easy to read anyone's code and immediately understand what's happening . Be careful : Of course , How much does it actually “ Easy to ” Depending on your use case . If you want to create a basic CRUD API, I still recommend Django + DRF or Rails.

These features are fun to play with , however , As most programmers would agree , When reading other people's works , They often make the code harder to understand .Go Force you to stick to the basics . This makes it easy to read anyone's code and immediately understand what's happening . Be careful : Of course , How much does it actually “ Easy to ” It depends on your scenario . If you want to create a basic CRUD API, I still recommend Django + DRF or Rails.

reason 4 – Concurrency and channels

As a language ,Go Trying to make things easy . It does not introduce many new concepts . The point is to create a simple language that is very fast and easy to use . Its only innovative area is goroutine And channel .(100% correct CSP The concept begins with 1977 year , So this innovation is more of a new approach to old ideas .)Goroutines yes Go Lightweight threading method , The channel is goroutines The preferred way to communicate between .Goroutines The creation of is very cheap , And it only takes a few KB Extra memory for . because Goroutine Very light weight , So it is possible to run hundreds or even thousands of at the same time . You can use channels in goroutine Communicate with each other .Go The runtime handles all complexity .goroutines And the channel - based concurrency approach makes it possible to use all available CPU Kernel and processing concurrency IO It's very easy —— None of this complicates development . And Python/Java comparison , stay goroutine Running the function on requires a minimum of sample code . You just need to add the keyword before the function call “go”:

https://tour.golang.org/concurrency/1 Go The concurrency method of is easy to use . And Node comparison , It's an interesting way , Developers must pay close attention to how asynchronous code is handled .Go Another important aspect of concurrency in is Competition detector . This makes it easy to determine if there are any race conditions in asynchronous code .

Here's how to start using Go And some good resources of the channel :

https://gobyexample.com/channels
https://tour.golang.org/concurrency/2
http://guzalexander.com/2013/12/06/golang-channels-tutorial.html
https://www.golang-book.com/books/intro/10
https://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html
Goroutines vs Green threads

reason 5 – Fast compile time

We are using Go The largest compilation of microservices written requires 4 second . Compared with the slow compilation Java and C++ Equal language comparison ,Go Fast compile time for is a major productivity victory .

I like swordsmanship , But it would be better to get things done while I still remember what the code should do :

reason 6 - Team building ability

First , Let's start with the obvious : And C++ and Java Compared with the old language ,Go The number of developers is small . according to StackOverflow The data of , 38% Of developers know Java, 19.3% Of People know C++, Only 4.6% Of People know Go. GitHub data Shows A similar trend :Go Than Erlang、Scala and Elixir And other languages are more widely used , But not as good as Java and C++ popular . Fortunately, ,Go It is a very easy language to learn . It provides the basic functionality you need , That's it . The new concept it introduces is “ Delay ” Declaration and built-in concurrency management “goroutines” And channel .( For purists :Go Is not the first language to implement these concepts , Just the first language that made them popular .) Anyone who joins the team Python、Elixir、C++、Scala or Java All developers can Go Get started , Because of its simplicity . Compared with many other languages , We found that build Go It's easier for a team of developers . If you recruit people in the ecosystem , This is an important advantage .

reason 7 – A strong ecosystem

For a team of our size ( about 20 people ) Come on , Ecosystem is very important . If you have to reinvent every little feature , You can't create value for your customers .

Go It has good support for the tools we use . Entity library is already available for Redis、RabbitMQ、PostgreSQL、 Template parsing 、 Task scheduling 、 Expression parsing and RocksDB. And Rust or Elixir And other newer languages ,Go The ecosystem is a major victory . It's certainly not as good as Java、Python or Node Such languages are good , But it's reliable , And for many basic needs , You will find that there are already high-quality packages available .

reason 8 – Gofmt, Force code formatting

Let's start from what is Gofmt Start ? No , This is not an oath .Gofmt Is a great command line utility , Built in Go In the compiler , For formatting code . In terms of function , It is associated with Python Of autopep8 Very similar . Even though 《 Silicon valley 》 In other ways , But most of us don't really like arguing about tabs and spaces . Format consistency is important , But the actual format standard is not so important .Gofmt Avoid all this discussion by formatting your code in a formal way .

reason 9 – gRPC And protocol buffer

Go Yes Protocol Buffers and gRPC With first-class support . These two tools are very suitable for building the need to pass RPC Microservices for communication . All you need to do is write a list , Define what can be done RPC Calls and the parameters they take . Then automatically generate the server and client code from this list . The generated code is fast , The network takes up less space , Easy to use . From the same list , You can even generate client code for many different languages , for example C++、Java、Python and Ruby. therefore , Internal traffic is no longer ambiguous REST Endpoint , And you have to write almost the same client and server code every time .

shortcoming 1 – Lack of framework

Go Didn't like Rails be used for Ruby、Django be used for Python or Laravel be used for PHP Such a single dominant framework . This is a Go A hotly debated topic in the community , Because many people argue that you should not use a framework from the beginning . I fully agree that this is true for some use cases . however , If someone wants to build a simple CRUD API, They will be easier to use Django/DJRF、Rails Laravel or Phoenix. to update : As the comment points out , There are several projects for Go Provides a framework . Revel , Iris , Echo , Macaron and Buffalo Seems to be the main competitor . about Stream The use case , We prefer not to use frameworks . However , For many want to provide simple CRUD API For new projects , The lack of a dominant framework would be a serious disadvantage .

shortcoming 2 – Error handling

Go Simply return an error from a function and expect the calling code to handle the error ( Or return it to the call stack ) To handle errors . Although this method works , But it's easy to lose the scope of the problem , To ensure that you can provide users with meaningful errors . Wrong package Solve this problem by allowing you to add context and stack trace to the error . Another problem is that it's easy to forget to handle errors . image errcheck and megacheck Such a static analysis tool can easily avoid these mistakes . Although these workarounds work well , But it doesn't feel right . You want the language to support correct error handling .

Inferiority 3—— Package management

to update : Since writing this article ,Go Package management has made great progress . Go modular It's an effective solution , The only problem I see is that they break some static analysis tools , Such as errcheck. This is a use of Go modules Learn to use Go A tutorial for . Go Package management is by no means perfect . By default , It cannot specify a specific version of a dependency , Nor can you create a reproducible build .Python、Node and Ruby There are better package management systems . however , Use the right tools ,Go Package management for works well . You can use Dep To manage your dependencies , To allow specifying and fixing versions . besides , We also contributed an open source tool called VirtualGo , It can be handled more easily with Go Multiple projects written .

Conclusion

Go Is a very high-performance language , Good support for concurrency . It's almost the same as C++ and Java As fast as language . Although with Python or Ruby comparison , Use Go Building things does take more time , But you'll save a lot of time optimizing your code . We are Stream There is a small development team , To exceed 5 Billion end users Provide power and Chat .Go Combined with the A strong ecosystem 、 For new developers Easy to get started 、 Fast performance 、 For concurrent Reliable support and an efficient programming environment , Make it a good choice . **Stream Still on our dashboard 、 Use in site and machine learning Python To provide Personalized feeds . We won't be with soon Python say goodbye , But in the future, all performance intensive code will use Go To write . Our new Chat API It is also completely used Go To write .** If you want to know about Go For more information , Check out the blog posts listed below .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved