This is a creation in Article, where the information may have evolved or changed. "Editor's note" Last year, Express.js library author TJ Holowaychuk moved from node. js to the Go language and published an article to compare the two languages. Now, there are more and more platforms to go to develop, for example, the CSDN research and Development channel last year released Bowery why abandon node. js, to go? So why did the timehop abandon rails to go?
The popular social app Timehop (Time Machine) can help people recall their own footprints on different social media one day, and the scale of their users has been expanding since the launch. What is the reason why Timehop's development platform has undergone a shift from rails to go?
Since the migration of Timehop to go has been almost 1.5, of which 95% of the response time from the rails era of 700ms reduced to 70ms, the number of active users per day is 6 million. Next I will summarize the experience of the go migration process, and hope to let everyone better understand the go language.
Why choose Go?
In the early days of operation, Timehop was running on rails. But with the number of users and the rapid increase in traffic, rails has gradually become a bottleneck problem. Many of our requests need to be processed in parallel. When the user launches Timehop, we need to collect all of the user's social data in the past. The data queries of different platforms are independent of each other, so parallel processing is a high-frequency event. We also tried to combine it with Ruby, but it didn't work well because Ruby wasn't really a multithreaded system.
In view of this, we have listed the most issues that need to be addressed in three new platforms:
- Can be easily processed in parallel
- Real multi-threaded system
- Performance
Why can go stand out?
In addition to Ruby,node.js also once entered our vision. But in the end we chose go:
- Performance: The go Code will be compiled into machine code, the compiled program can run at a speed comparable to C + +, and no additional virtual machines and parsers;
- Static writing: The computer can master all the writing errors and we can't. Why not rely on the computer to deal with it?
- Robust and straightforward parallel processing: go programs and pipelines make parallel code readable and clear. It also makes parallel code more secure without explicit locking, and there's no need to worry about callback confusion.
As the migration progresses, we also find more advantages:
- Very simple deployment work-When compiling go program, the program itself and all program dependencies will be compiled into a single binary file;
- A rich set of tools-such as the Code format tool "Go FMT"-can help developers eliminate the problem of non-Uniform code formats. In addition go has many powerful tools;
- Rich standard library-with go, many of the third libraries are no longer needed.
We can see that go is in line with our previous three-point request, so we finally chose it. But at the same time, the team has also discussed the Go language, now organized as follows.
is there an accident when I use go?
What we fear most when migrating is the productivity that affects program code. Before using ruby encoding, the speed was very fast. But after use, it is found that go not only has no effect on writing speed, but also is more robust and can help reduce many input errors. The compilation speed is also satisfactory, and one of our largest go projects can be completed in as much as 2.5s.
How to complete the team technology switch?
In combination with personal experience, I have published an introductory article for the team to read. In addition, go tutorial, the standard library of efficient Go,go official website are very good textbooks.
Go has weaknesses?
Management of program dependencies. Go provides a convenient way to import packages in a path-based way. For example:
Import "Github.com/timehop/golog/log"
You can use the go get command to run. But while it is convenient, if someone else changes the path or provides a change to the API, it can cause an unknown deployment error. Therefore, we recommend that you use the deployment tool GODEP to manage program dependencies.
What are the key libraries to be aware of when deploying?
We tried to search for information on the go on rails, but nothing. At the moment, we are using the standard Net/http library as well as the gorilla Mux. This is also the current mainstream approach.
How is it managed? How do I deploy it?
Initially hosted on the same heroku as rails and deployed using go buildback. Later migrated to EC2, the deployment was just as easy. As mentioned above, the GO program is ultimately a compiled binary file.
Our current process is:
1. Upload the code to GitHub;
2. Use Travis for testing;
3. If passed, compile, compress, and upload to S3.
When you deploy, download the tar file from S3, unzip it, and then run it. (using chef)
can I use the Go language to create APIs?
OK. For example, a user struct:
Type User struct { FirstName string ' JSON: ' first_name ' ' LastName string ' JSON: ' last_name ' ' Password string ' JSON: '-' '}
The label after the user information item defines how JSON serialization is done.
If you want to output a user instance, you can simply write it as:
U: = user{firstname: "Abe", LastName: "Dino", Password: "P4ssw0rd"}jsonbytes, _: = json. Marshal (U)
How is the GO program handled in terms of polymorphism and modularity?
Go is not an object-oriented language, and there is no type hierarchy. There is no inheritance at the same time, can be free to carry out class inclusion, so as to achieve polymorphism and code reuse.
In addition the go interface is very friendly. Unlike other languages that require explicit class declarations, a simple struct in go can be used. We feel that this is one of its most powerful features, for specific reasons can be referred to here.
How important is Google's support?
To continue to get Google's help and input, the go language is undoubtedly a great boon. The size of the community has also been expanding, resulting in a number of high-quality open source projects and reference resources.
written in the last
We hope this article will help the team interested in the go language. To get the most out of the new platform is a great time to work with team members to think more and discuss more to ensure the project goes smoothly.
English from: Medium