Someone gave up Python and switched to the Go language for nine reasons, pythongo.

Source: Internet
Author: User
Tags cassandra

Someone gave up Python and switched to the Go language for nine reasons, pythongo.

Switching to a new language is usually a big decision, especially when only one of your team members has used it. This year, the main programming languages of the Stream team have switched from Python to Go. This article explains the nine main reasons behind it and how to make a proper transition.

I. Why do I use Go?

Cause 1: Performance

Go is extremely fast. Its performance is similar to that of Java or C ++. In our use, Go is generally 30 times faster than Python. The following is a benchmark comparison between Go and Java:

Cause 2: Language performance is important

For many applications, the programming language simply serves as the glue between it and the dataset. The performance of the language is often irrelevant.

However, Stream is an API provider serving more than 500 end users in the world's top 0.2 billion. Over the years, we have optimized Cassandra, PostgreSQL, Redis, and so on, but eventually reached the limit of the language used.

Python is great, but it does not perform well in serialization/de-serialization, sorting, and collection. We often encounter this problem: Cassandra retrieves data in 1 ms, but Python needs 10 ms to convert it into an object.

Cause 3: developer efficiency & do not be too innovative

Take a look at the excellent getting started tutorial "getting started with the Go language"

A small piece of code in (http://howistart.org/posts/go/1:

If you are a newbie, you will not be surprised to see this code. It displays a variety of assignments, data structures, pointers, formatting, and built-in HTTP libraries.

When I was programming for the first time, I liked using Python's advanced functions. Python allows you to use the code being written creatively. For example, you can:

During code initialization, you can use MetaClasses to manually register a category replacement to add functions to the built-in function list by using the wonderful method to overload operators.

Without a doubt, this code is very interesting, but it also makes it hard to understand when reading the work of others.

Go forces you to stick to the Foundation, which facilitates reading arbitrary code and quickly understands what is happening.

Note: Of course, how easy it is depends on your use case. If you want to create a basic crud api, I suggest using Django + DRF or Rails.

Cause 4: concurrency & Channel

As a language, Go is committed to simplifying things. It does not introduce many new concepts, but focuses on creating a simple language. It is very fast and simple to use. Its only innovation is goroutines and channels. Goroutines is a lightweight Go thread-oriented method, and the channel is the priority for communication between goroutines.

The cost of creating Goroutines is very low. It only requires several thousand bytes of extra memory. This makes it possible to run hundreds or even thousands of goroutines at the same time. You can use channels to implement communication between goroutines. The Go running time can indicate all complexity. Goroutines and channel-based concurrency methods make it very easy to use all available CPU cores and handle concurrent IO-all with no complex development. Compared with Python/Java, running a function on a goroutine requires the smallest sample code. You only need to use the keyword "go" to add function calls:

package main import (  "fmt"  "time")func say(s string) {  for i := 0; i < 5; i++ {   time.Sleep(100 * time.Millisecond)   fmt.Println(s)  }}func main() {  go say("world")  say("hello")} 

The concurrency method of Go is very easy to use and interesting compared with Node. In Node, developers must pay close attention to asynchronous code processing.

Another excellent feature of concurrency is competition detector, which makes it easy to figure out whether there are race conditions in asynchronous code. Below are some good resources for getting started with Go and channels:

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

Cause 5: Quick Compilation Time

Currently, it takes only 6 seconds to compile the largest microservice compiled using Go. Compared with the slow Compilation speed of Java and C ++, the quick Compilation Time of Go is a major efficiency advantage. I love fencing, but when I still remember what the code should do, it will be better if it is done.

Code compilation before Go

Cause 6: build team capabilities

First, the most obvious point is that Go developers are far from many old languages such as C ++ and Java. 38% of developers know Java, 19.3% know C ++, and only 4.6% know Go. GitHub data table shows similar trends: Go is more popular than Erlang, Scala, and Elixir, but it is no longer compared to Java and C ++.

Fortunately, Go is very simple and easy to learn. It only provides basic functions and is not redundant. The new concept introduced by Go is the "defer" Statement and built-in concurrency management with goroutines and channels. Thanks to the simplicity of Go, any Python, Elixir, C ++, Scala, or Java developer can form an efficient Go team in January.

Cause 7: Powerful Ecosystem

The ecosystem is important for a team of about 20 members of this size. If you need to redo each function, you cannot create benefits for customers. Go has powerful tool support for stable databases for Redis, RabbitMQ, PostgreSQL, Template parsing, Task scheduling, Expression parsing, and RocksDB.

The Go ecosystem has great advantages over languages such as Rust and Elixir. Of course, it is slightly inferior to languages such as Java, Python, or Node, but it is very stable, and you will find that there are already high quality file packages available in many basic requirements.

Cause 8: GOFMT, forced code format

Gofmt is a powerful command line function that is built in the Go compiler to specify the code format. In terms of function, it is similar to Python's autopep8. Format consistency is important, but the actual format standards are not always important. Gofmt uses an official specification code to avoid unnecessary discussions.

Cause 9: gRPC and Protocol Buffers

The Go language provides first-class support for protocol buffers and gRPC. These two tools work together to build microservices that need to communicate through RPC ). We only need to write a list (manifest) to define the RPC call situation and parameters. Then, the server and client code will be automatically generated from this list. In this way, code generation is not only fast, but the network usage is also very small.

From the same list, we can generate client code from different languages, such as C ++, Java, Python, and Ruby. Therefore, the RESET endpoints of internal communication do not have differences. Every time we need to write almost the same client and server code.

Ii. disadvantages of using the Go Language

Disadvantage 1: lack of Framework

The Go language does not have a major framework, such as the Ruby Rails framework, the Python Django framework, or the PHP Laravel. This is a heated discussion in the Go language community, because many people think that we should not start from using the framework. This is true in many cases, but if you just want to build a simple crud api, using Django/DJRF, Rails Laravel, or Phoenix will be much simpler.

Disadvantage 2: Error Handling

The Go language helps developers handle compilation errors by simply returning errors (or calling stacks) through functions and expected call codes. Although this method is effective, it is easy to lose the scope of errors, so it is difficult to provide users with meaningful error information. Errors package allows us to add the context and stack trace of the returned error to solve this problem.

Another problem is that we may forget to handle the error. Static analysis tools such as errcheck and megacheck can avoid these errors. Although these solutions are very effective, they may not be so correct.

Disadvantage 3: software package management

Software Package management in the Go language is definitely not perfect. By default, it cannot create dependency libraries of specific versions, nor can it create writable builds. In contrast, Python, Node, and Ruby all have better software package management systems. However, the software package management in the Go language can also be good with the correct tools.

We can use Dep to manage dependencies. It can also specify a specific software package version. In addition, we can also use an open-source tool named VirtualGo, which can easily manage multiple projects written in the Go language.

Python vs Go

An interesting experiment we implemented was to write the ranking feed using Python and then rewrite it using Go. Let's take a look at the following sorting method example:

Code

Python and Go code both need the following requirements to support the above sorting method:

The expression of resolution score. In this example, we want to convert simple_gauss (time) * popularity string into a function that can take activity as input and then give a score as output. Create some functions on JSON config. For example, we want simple_gauss to call decay_gauss, and the key-value pairs include "scale": "5d", "offset": "1d", and "decay ": "0.3 ". Parse the "ults" configuration to facilitate feedback when a domain is not clearly defined. Use the function from step 1 to score all the activities in the feed.

It takes about three days to develop the Python version sorting code, including writing code, testing, and document creation. Next, I spent about two weeks optimizing the code. One of the optimizations is to translate the score expression simple_gauss (time) * popularity into an abstract syntax tree. We have also implemented caching logic, which will calculate the score each time in advance.

In contrast, it takes four days to develop the code for the Go version, but no more optimization is required later. So although Python was initially developed faster, Go eventually requires less work. In addition, the Go code is more than 40 times faster than the highly optimized python code.

The above is just one of the benefits we have experienced when turning to Go. Of course, it cannot be compared as follows:

This sort code is the first project I wrote with Go. the Go code was written after the Python code, so I understood the case in advance. The quality of the expression Parsing Library of Go is superior.

Elixir vs Go

Another language we evaluate is Elixir. Elixir is built on the Erlang virtual machine. This is a fascinating language, and we think of it because one of our team members is very experienced on Erlang.

In the use case, we observe that the original performance of Go is better. Both Go and Elixir can well handle thousands of parallel requests. However, if it is a separate requirement, Go is actually faster. Another reason we choose Go over Elixir is the ecosystem. On the components we need, the Go library is more mature. In many cases, the Elixir library is not suitable for products. At the same time, it is difficult to find/train developers who also use Elixir.

Conclusion

Go is a highly efficient language that supports high concurrency. At the same time, it is as fast as C ++ and Java. Although it takes more time to create things using Go than Python and Ruby, it can save a lot of time in subsequent code optimization. At Stream, we have a small development team that provides a feed Stream for 0.2 billion end users. For novice developers, Go integrates a powerful ecosystem and is easy to use. It also features extremely fast performance and high concurrency support. It has become a good choice in a productive programming environment. Stream still uses Python for personalized feed, but all performance-intensive code will be written in Go.

Summary

The above is a small series of people who gave up Python and switched to the Go language for nine reasons. I hope to help you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.