Go was invented at Google and written by three computer gurus, Rob Pike, Ken Thompson, and Robert Griesemer. Due to its pedigree, Go attracted a lot of attention from developers when it was born. In the decade since it was born, there have been a number of go-based applications. Not long ago, Zhihu ditched Python and used Go to rebuild its recommendation system.

Author: huawei cloud homestead link: https://zhuanlan.zhihu.com/p/57895717

Jake Wilson, a former Google employee, believes that Go has many better features than Python, and can replace Python in many cases. He has used Go instead of Python in many tasks. So what is the unique appeal of Go? What does it do better than Python? Let’s take a look!

The programming languages programmers most want to learn in 2019

Go language from a prestigious family

For a new language with a history of only ten years, the development momentum of Go is quite rapid. Docker, the carrier of container industry, was written with Go, and many teams in China have widely used Go. HackerRank recently launched a community survey of programmer skills, with more than 70,000 developers from over 100 countries participating. According to the survey, the Top 3 programming languages that programmers want to learn in 2019 are Go, Kotlin and Python, with Go topping the list at 37.2%.

But Go will have a hard time unseating Java, the programming evergreen that has held sway for more than two decades. According to HackerRank, Java is still the # 2 most popular programming language for developers in 2018, followed by Python at # 4 and Go at # 13, not far behind JavaScript.

But for author Jack Wilson, Go is “still young” but already has a lot of great features.

What’s so good about Go?

I’ve used Go instead of Python for many tasks, to give you a few examples:

  • Process cloud logs stored on S3
  • Move tB-level files between buckets and/or regions on S3
  • Match local database records with files on S3 to keep files in sync

Most of these tasks are one-off, so scripting languages are appropriate. These tasks require quick programming, and code used once is often discarded. In general, the requirements of such tasks are novel and specialized, and the code rarely needs to be reused. Here’s why Go can be used instead of Python for this task.

It’s convenient to have a compiler

I often make very elementary mistakes when writing Python. I give variables or functions the wrong name, or pass them the wrong arguments. Some of these errors can be found with a debugging tool, but such tools generally require special Settings. I’ve never configured PyLint very easily, and I don’t like using heavy ides that are more difficult to configure. The worst thing you can do is accidentally type the wrong variable name, which is not easy to spot. Your script might run for hours before it hits this error, then everything crashes and you have to run the script all over again. Most of these errors can be detected by unit tests, but unit tests rarely cover 100% of the code, and I don’t want to waste time writing unit tests for a once-only script. A language with a compiler solves all of these problems. ** The compiler can detect all your silly mistakes. ** For this reason, I prefer to use languages like Go when writing code that is hundreds of lines long.

Speed of development

However, one disadvantage of languages that need to be compiled is that your development speed will generally slow down. This is especially true in languages such as C/C++ and Java. Go is a very simple language, and I haven’t found it slowed down much. Don’t get me wrong, I’m not saying it’s faster than Python, but that ** With Go it’s not much slower than Python, and it’s generally fine to get up to 85% of Python development speed. ** I think it’s worth sacrificing 15% of your development speed for all the silly errors you can avoid by having a compiler.

Better parallelism

As you probably already know, the Go language is built for parallelism. Parallel programs are often needed in my team because we need to manipulate large amounts of data in our database on S3. If the task is IO intensive (and many tasks are), we can easily deploy Python threads. But if the task is CPU-intensive, Python is less convenient because of the global interpreter lock. I really enjoy the ease with which simple code can run in multiple threads without modification in Go. Have you ever run into this problem in Python: copy-and-paste multithreaded code that doesn’t work at all? This problem is not present in Go.

The deployment of simple

I prefer to keep all dependencies in a single binary file. I often run my own scripts on the EC2 server to make the environment more similar to our server on S3. With Python, I need to make sure that all required packages are installed on the server, and that my colleagues can’t install any packages on the server that might cause conflicts. Virtual environments solve most of these problems, but I still find it easier to use Go. I typically cross-compile my code on Mac and Linux, copy it to a remote server, and let it run. All the dependencies my code needs are in one binary file.

Style is consistent

At first, gofmt, the formatting tool for The Go language, really drove me crazy, especially when it required the TAB key instead of the space bar for indentation. I think it’s crazy. But after I used it for a while, it started to smell really good. When I write code, I can be creative with my formatting, and the formatting tool does everything for me. All of my code styles are consistent, even if I’m writing different projects. This is because formatting is a feature of the standard Go tool. But if I wanted to do this in Python, IT would take some effort. I need to configure the PyLint tool correctly and make sure I use it in every project.

More convenient tools

Gofmt is just one small example of the many tools available for the Go language. All of my favorite editors — VSCode, vim, and Sublime Text — have extensions for the Go language that make it easy for me to take advantage of the Go tool. This way, I can get the same kind of intelligent experience I had when I wrote Java, without actually having to use an IDE. I’ve never had this experience with Python.

Go certainly has its drawbacks

Almost every time I see an article criticizing Go, it’s about the language’s lack of key features, such as generics. I don’t think the lack of generics matters — you’ll find that you can do an amazing amount of things with Maps and slices. But I ran into a lot of other problems with Go.

Inflexibility

First of all, Go is probably the most “stubborn” language I’ve ever spoken. For example, it forces you to use TAB instead of the space bar for indentation (assuming you use Gofmt), to use specific file organization, to program in GOPATH environment variables, and so on. There are too many features of the language to change. One of the reasons Go is so easy to learn is that you can’t change these features. Sorry if you don’t want to export all the variable names with uppercase letters. Fortunately, these features of Go do not offend my boundaries, but I can understand if some of the requirements are completely unreasonable. Python is much more flexible.

Library support is a bit lame

It is unfair to compare Go to Python in this respect. Go came a long way after Python, but I was still confused when I found some features that Go didn’t support. I’ve even seen a lot of people on StackOverflow post code snippets that should be built in, and everyone needs that feature and copy-pastes it into their own projects. Shouldn’t this kind of functionality be embedded in the language? Two examples from recent years come to mind:

  • Sorting slices (fortunately this is much easier in Go 1.8)
  • Math.round only supports integers and cannot round floating-point numbers (for example, if you want to find the nearest integer to 0.5, Go cannot do this). Even before Go 1.10, there was no math.round function at all

Of course, part of the problem is that Go doesn’t have generics, and part of the problem is that Go developers only added the most essential features to Go’s standard library.

I understand both points, but I still get annoyed when I have to write my own code to solve small problems. Hopefully, as the Go language develops, it will become less and less problematic.

Who do you like better, Go or Python? Welcome message exchange!

Before Golang, I used C/C++, Lua, and Python as my main development languages.

C/C++ problems:

  • Low development efficiency and high requirements for developers
  • Libc is only backward compatible and difficult to operate and maintain

Lua/Python problems:

  • Dynamic language, lack of compilation process, low-level errors frequently
  • Lack of effective performance analysis and debugging tools

** 场景**

At that time, I just finished the development of Nginx WAF module, and began to set up the background management system of WAF. Since my colleagues used Fluentd as the log collection component, I also chose Fluentd to keep the consistency of the basic components. That is, the final structure is Fluentd ->mongodb->mysql, and then do front-end data display based on mysql. Fluentd was rewritten with Go to solve the following problems:

  • Fluentd occasionally appears to fake death in Ubuntu 9.04, resulting in data loss
  • Fluentd is difficult to access the company’s existing package distribution system, resulting in great difficulty in operation and maintenance
  • Mongodb is implemented using Mmap, which occupies too much memory when there is a large amount of data

plan

  • The code for GoFluent can be found here
  • The HTTPMQ code can be found here

In fact, both projects are aimed at solving the problems mentioned above. Believe it or not, I finished these two sets of code in my spare time, which means it is not in KPI at all. In fact, I did not expect to write so quickly, after all, it is learning to use. But the reality is that IT took me a week to write HTTPMQ and a little over a month to write GoFluent… Of course, both projects leave much to be desired. For now, I prefer elastic/ Logstash -forwarder · GitHub and elastic/ Logstash · GitHub.

Why Golang

So why did I choose Golang? I had spent a lot of time doing a lot of research before I made this choice. Foreign companies such as Google, AWS, Cloudflare and CoreOS, as well as domestic companies such as Qiniu and Ali, have started to use Golang to develop their cloud computing products on a large scale. Following the footsteps of world-class giants should not lead me in the wrong direction, and in the process of learning Golang, I was gradually impressed by the design philosophy behind it.

In addition, yunfeng blog once said such a sentence:

I found that I spent four years honing my ability to build systems in C, trying to find a specification that would make it easier to write software. Turns out it was just a copy of Go. Lack of language level support, can only be a parody.

Here’s what I learned about Golang:

  • With C foundation, it is very easy to learn Golang
  • Synchronous mode easily achieves high concurrency
  • Simple code, unified format, easy to read
  • Performance is strong, but development efficiency is not worse than dynamic languages such as Python

The effect

At the beginning of the preparation of the line when in fact very uneasy, after all, once the failure, not only get their own back, face also can not pass ah. Fortunately, the results are pretty beautiful. Since the launch, there has not been a sudden BUG, which reduces the difficulty of operation and maintenance while reducing the load of the machine.

All in all, Golang is a smart choice from an engineering standpoint for most backend applications. This makes it easy to balance the three singularities of performance, development efficiency, and maintenance that make many programs so crazy.

To read more

Capital winter under the Android surface

When programming, is it efficient to type in someone else’s code?

After five years of android development, I started my go learning journey

2018 Developer Ecology Report: Java is the most popular, Go has the most potential, and JavaScript is the most used

Finally, I still suggest you in this Internet era, do not deliberately pursue a language, each language has his advantages, choose the right is the most important. The Internet has no outdated technology and no cold winters. We choose to store as much grain firewood as possible. Welcome to follow my wechat technical number: Terminal R&d Department, ID: codeGoogler. If you have any problems, you can also communicate with me ~