Why did I give up Python and chose Go?

Source: Internet
Author: User
Tags cassandra serialization

Based on Wikipedia statistics (https://en.wikipedia.org/wiki/List_of_programming_languages)
A total of more than 600 different programming languages. For a variety of development languages, each language has its own pros and cons. and inspirational to become a knowledgeable and wide-range of developers, the industry also has specialized, Zennai will not learn to finish hundreds of languages.

In these languages, Java as the 22 ivy, whether it is a new outbreak of Kotlin or C, C + +, Python, JavaScript and other old xxx language want to be in the short-term to shake their status is not realistic, I believe this is beyond doubt. After eating i ann from the TIOBE programming language rankings in recent months, after Java, C, C + +, C # Use rate has declined, but still sit on the top five rankings.

In addition to the above four kinds of head XXX language, there is also a Python (python). With the rise of artificial intelligence, Internet of Things, data science and other fields, Python is rapidly warming up and becoming the preferred entry language for many developers. However, in a follow-up sound, many developers from Python to Go language, what is the reason? Next, Thierry Schellenbach from the Stream team gave nine reasons and compared Python, parsing the pros and cons, hoping to help more developers.

Why do I start using the Go language?

Reason for the performance

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

Cause 2: Language performance is important

For many applications, the programming language simply acts as a glue between it and the data set. The performance of the language itself is often irrelevant.

But Stream is an API provider serving the world's top 500 and over 200 million end users. Over the years we have optimized Cassandra, PostgreSQL, Redis, and so on, but eventually arrived at the limits of the language used.

Python is great, but it behaves poorly in serialization/de-serialization, sorting, and aggregation. We often encounter this problem: Cassandra time 1ms retrieves the data, but Python needs 10ms to convert it into an object.

Reason 3: Developer efficiency & not too innovative

Take a look at the great introductory tutorial "Getting started with the Go language" (http://howistart.org/posts/go/1/)
A small piece of code in:

Package Main

Type Openweathermap struct{}

Func (w openweathermap) temperature (city string) (Float64, error) {

    resp, err := http.Get("http://api.openweathermap.org/data/2.5/weather?APPID=YOUR_API_KEY&q=" + city)    if err != nil {            return 0, err    }    defer resp.Body.Close()    var d struct {            Main struct {                    Kelvin float64 `json:"temp"`            } `json:"main"`    }    if err := json.NewDecoder(resp.Body).Decode(&d); err != nil {            return 0, err    }    log.Printf("openWeatherMap: %s: %.2f", city, d.Main.Kelvin)    return d.Main.Kelvin, nil

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

When I first programmed, I enjoyed using Python's higher-order features. Python allows you to creatively use the code you are writing, for example, you can:

When code is initialized, use metaclasses to register the category yourself

Replacement true and False

Add a function to the list of built-in functions

Using the marvelous method of overloading operators

This code is undoubtedly interesting, but it makes the code difficult to understand when reading other people's work.

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

Note: Of course, how easy it is depends on your use case. If you're going to create a basic CRUD API, I suggest you use Django + DRF, or Rails.

Cause 4: Concurrency & Channel

Go is a language dedicated to making things simple. Instead of introducing many new concepts, it focuses on creating a simple language that is extremely fast and simple to use. Its only innovation is the Goroutines and the channel. Goroutines is a lightweight way to Go thread-oriented, and a channel is a priority for communication between goroutines.

The cost of creating the goroutines is very low, requiring only thousands of bytes of extra memory, which makes it possible to run hundreds of or even thousands of goroutines at the same time. You can use the channel to achieve communication between goroutines. Go run time can represent all the complexities. The Goroutines and channel-based concurrency methods make it very easy to use all available CPU cores and handle the concurrency of io--all without complex development. Running a function on a goroutine requires minimal boilerplate code compared to Python/java. You simply add a function call using the keyword "go":

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")

}

Https://tour.golang.org/concurrency/1

The concurrency method of Go is very easy to get started with and interesting compared to node, and in node, developers have to pay close attention to the processing of asynchronous code.

Another high-quality feature of concurrency is the race detector, which makes it easy to figure out whether there are race conditions in the async code. Here are some good resources to get 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: Fast compile time

The compilation time of the largest microservices we have written with Go is now only 6 seconds. The fast compile time of Go is a major efficiency advantage compared to the sluggish compilation speed of Java and C + +. I love fencing, but when I still remember what the code was supposed to do, it was better to be done.

Code compilation prior to Go

Reason 6: The ability to build a team

First of all, the most obvious point is that Go developers are far from having many older languages such as C + + and Java. It is known that 38% of developers understand java,19.3% developers understand C + +, only 4.6% of developers know Go. The GitHub data shows a similar trend: more popular than Erlang, Scala, and Elixir,go, but not compared to Java and C + +.

Fortunately, Go is simple and easy to learn. It provides only the basic functionality and is not redundant. The new concept that Go introduces is the "defer" declaration, as well as the 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 within January.

Cause 7: A powerful ecosystem

Ecosystems are important to our size team (about 20 people). If you need to redo each piece of functionality, you won't be able to generate revenue for the customer. Go has powerful tool support for a stable library of Redis, RabbitMQ, PostgreSQL, Template parsing, Task scheduling, Expression parsing, and Rocksdb.

The Go ecosystem has a great advantage over languages such as Rust and Elixir. Of course, it's slightly less of a language like Java, Python, or Node, but it's stable, and you'll find that there are already high-quality packages available on many basic requirements.

Reason 8:GOFMT, forcing code format

GOFMT is a powerful command-line feature built into the Go compiler to specify the format of the code. Functionally, it resembles Python's autopep8. Formatting consistency is important, but the actual formatting standards are not always very important. GOFMT uses an official form of specification code to avoid unnecessary discussion.


Cause 9:GRPC and Protocol buffers

The Go language has first-class support for protocol buffers and GRPC. The two tools work together in a friendly way to build a micro server (microservices) that needs to communicate over RPC. We just need to write a list (manifest) to define what happens to the RPC call and the parameters, and then from that manifest will automatically generate the server and client code. This produces code that is not only fast, but also consumes very little network.

From the same list, we can generate client code from different languages, such as C + +, Java, Python, and Ruby. So the RESET endpoint for internal communications does not diverge, and we need to write almost the same client and server code every time.

Disadvantages of using the Go language

Disadvantage 1: Missing frames

The Go language does not have a major framework, such as the Ruby Rails framework, Python's Django framework, or PHP Laravel. This is a hotly debated issue in the Go language community because many people think that we should not start with the use of frameworks. This is true in many case scenarios, 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 to handle compilation errors by simply returning an error (or returning the call stack) through the function and the expected calling code. Although this method is effective, it is easy to lose the scope of the error, so it is difficult for us to provide meaningful error messages to the user. The error packet (Errors package) allows us to add context and stack traces that return errors to resolve the problem.

Another problem is that we may forget to deal with the error. Static analysis tools such as Errcheck and Megacheck can avoid these errors. While these solutions are very effective, they may not be the right approach.

Disadvantage 3: Package management

The package management of the Go language is definitely not perfect. By default, it has no way of making a specific version of a dependent library or creating a builds that can be replicated. In contrast, Python, Node, and Ruby have better package management systems. However, with the right tools, the Go language package management can also perform well.

We can use DEP to manage dependencies, and it can also specify a specific package version. In addition, we can also use an open source tool called Virtualgo, which makes it easy to manage multiple projects written in the Go language.


Python vs Go

One interesting experiment we implemented was to write a ranking feed in Python and then rewrite it with Go. Let's look at an example of this sort method:

{

    "functions": {            "simple_gauss": {                    "base": "decay_gauss",                    "scale": "5d",                    "offset": "1d",                    "decay": "0.3"            },            "popularity_gauss": {                    "base": "decay_gauss",                    "scale": "100",                    "offset": "5",                    "decay": "0.5"            }    },    "defaults": {            "popularity": 1    },    "score": "simple_gauss(time)*popularity"

}

Both Python and Go code require the following requirements to support the sorting method above:

Parse the expression of the score. In this example, we want to convert the Simple_gauss (time) *popularity string into a function that takes activity as input and then gives the score as the output.

Create a partial function on the JSON config. For example, we want "simple_gauss" to call "decay_gauss" with a key-value pair of "scale": "5d", "offset": "1d", "decay": "0.3".

Parse the "defaults" configuration to allow feedback in situations where a domain is not clearly defined.

Start using the function from STEP1 to rate all activity in the feed.

It takes about 3 days to develop a Python version sort code, including writing code, testing, and building documentation. Next, I'll spend about 2 weeks to optimize the code. One of the optimizations is to translate the score expression Simple_gauss (time) *popularity into an abstract syntax tree. We also implemented caching logic, which will then calculate each score in advance.

In contrast, it takes 4 days to develop the Go version of the code, but no more optimizations are needed afterwards. So while the initial development of Python is faster, the Go ultimately requires less work. In addition, the go code is 40 times faster than highly optimized Python code.

These are just a few of the benefits we've experienced in turning to Go. Of course, it is not possible to compare this:

The sort code is the first item I wrote with Go;

The Go code is written after the Python code, so the case is understood in advance;

Go's expression Analysis library is superior in quality.

Elixir vs Go

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

In the use case, we observed that go has a better raw performance. Both go and Elixir handle thousands of parallel requirements well, but if it's a separate requirement, go is actually faster. The other reason we chose Go with respect to Elixir is the ecosystem. The Go library is more mature on the components we need. In many cases, the Elixir library is not suitable for product use. At the same time, it is difficult to find/train developers who also use Elixir.


Conclusion

Go is a very efficient language and highly supports concurrency. At the same time, it is as fast as C + + and Java. While using Go to build things takes more time than Python and Ruby, you can save a lot of time on subsequent code optimizations. At stream, we have a small development team that provides feed streams to 200 million of end users. For novice developers, Go combines a powerful ecosystem, is easy to get started, also has ultra-fast performance, highly support concurrency, and a productive programming environment makes it a good choice. Stream still uses Python to personalize the feed, but all the performance-intensive code will be written in Go.

Why did I give up Python and chose Go?

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.