In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Why PHP programmers want to learn GO language, I believe that many inexperienced people do not know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Many people call GO the C language of the 21st century, because GO not only has the simplicity and performance of C, but also provides a variety of practical features of server development in the 21st century Internet environment, so that developers can easily get what they want at the language level.
History of development
In September 2007, Rob Pike compiled C++ on the Google distributed compilation platform. During the long wait, he and Robert Griesemer discussed some key issues of programming languages, and they thought that simplifying programming languages would be a bigger step forward than adding new features to bloated languages. Then they convinced the Ken Thompson around them before the compilation was finished and felt the need to do something about it. A few days later, they launched a project called Golang as a free-time experimental project. In May 2008, Google discovered the great potential of the GO language and received the full support of Google, and these people began to devote themselves to the design and development of the GO language full-time. The first version of the GO language was released in November 2009. The first official version of Go1.0 was released in March 2012. Go1.5 was released in August 2015, and this version is considered historic. Completely remove the C language part, compile GO with GO, and implement a small amount of code using assembly. In addition, they invited Rick Hudson, an authoritative expert in memory management, to redesign GC to support concurrent GC and solve the widely criticized problem of GC delay (STW). And in later versions, GC has been further optimized. When it comes to go1.8, the GC latency in the same business scenario can be controlled within 1ms from a few seconds of go1.1. To solve the GC problem, it can be said that the GO language has erased almost all the weaknesses in server-side development.
During the iteration of the go language version, the language features basically did not change much, basically remained on the GO1.1 benchmark, and officially promised that the new version was fully compatible with the code developed under the old version. In fact, the GO development team is very cautious about adding new language features, while constantly optimizing in terms of stability, compilation speed, execution efficiency, and GC performance.
Development team
The development camp of GO language can be said to be unprecedentedly powerful, and there is no lack of historical figures in the field of computer software among the main members, which has a far-reaching impact on the development of computer software. Ken Thompson, from Bell Labs, designed the B language and created the Unix operating system (originally implemented in B language). Then in the process of Unix development, C language was designed together with Dennis Ritchie, and then the Unix operating system was reconstructed using C language. Dennis Ritchie and Ken Thompson are known as the fathers of Unix and C, and were jointly awarded the Turing Award in 1983 for their outstanding contributions to the development of computer software. Rob Pike, also from Bell Labs, a key member of the Unix team, invented the Limbo language and co-designed the UTF-8 code with Ken Thompson, one of the authors of "Unix programming Environment" and "programming practice".
It can be said that the GO language relies on the big tree of Google, and there is no lack of awesome people, so it is a veritable "Niu second generation".
The famous Docker is implemented entirely in GO. The most popular container orchestration management system in the industry, kubernetes, is implemented entirely in GO, and then Docker Swarm is implemented entirely in GO. In addition, there are a variety of well-known projects such as etcd/consul/flannel and so on, all of which are implemented in GO. Some people say that the GO language is famous for catching up with the cloud era, but why can't we put it another way? it is also the GO language that promotes the development of the cloud.
In addition to cloud projects, there are companies such as Jinri Toutiao and UBER, who have also thoroughly restructured their business using the go language.
Key features of GO language
The reason why GO language is powerful is that it can always grasp the pain point of programmers in server-side development and solve problems in the most direct, simple, efficient and stable way. Here we will not discuss the specific syntax of GO language in depth, but will only introduce the key aspects of the language that are of great significance to simplifying programming, and follow in the footsteps of the masters to experience the design philosophy of GO.
The key features of the GO language include the following:
Concurrency and cooperation communication based on message passing rich and practical built-in data type function multiple return value defer mechanism reflection (reflect) high performance HTTP Server project management programming specification
In today's multi-core era, the significance of concurrent programming is self-evident. Of course, many languages support multithreaded, multiprocess programming, but unfortunately, implementation and control is not so easy and enjoyable. Unlike Golang, the language level supports goroutine concurrency (also known as microthreading, which is lighter, less expensive, and higher performance than threads). It is very easy to operate, and the language level provides keywords (go) to start collaborators, and thousands of collaborators can be started on the same machine.
Compared with the multi-thread implementation of JAVA and the co-program implementation of GO, it is obviously more direct and simple. This is the charm of GO, solving problems in a simple and efficient way, and the keyword go is perhaps the most important symbol of the GO language.
Communication mode based on message passing
In the process of asynchronous and concurrent programming, it is not enough to start the cooperative process conveniently and quickly. The message communication between cooperative programs is also a very important link, otherwise, each cooperative program will become a runaway Mustang and can not be controlled. In go language, communication based on message passing (rather than shared memory) is used for inter-program communication, and message pipeline (channel) is used as the basic data type, defined by type keyword (chan), which is thread-safe for concurrent operations. This is also revolutionary in the implementation of the language. It can be seen that the GO language itself is not simple without a bottom line, and it is precisely that they will provide users with the most practical and problem-solving capabilities in the simplest and most direct form.
Channel is not only used for simple message communication, but also can lead to a lot of very practical and convenient functions. For example, implementing TCP connection pooling, current limiting, and so on, which are not easy to implement in other languages, can be easily done by the GO language.
As a compiled language, GO also supports very comprehensive data types, in addition to the traditional integer, floating point, character, array, structure and other types. From a practical point of view, there is also native support for string type, slice type (variable length array), dictionary type, plural type, error type, pipe type, and even any type (Interface {}), and it is very easy to use. For example, string, slice type, the simplicity of operation is almost similar to python.
In addition, the error type (error) is used as the basic data type, and try is no longer supported at the language level. The use of catch should be regarded as a very bold and revolutionary initiative, and it is no wonder that many people complain that the GO language is irrelevant. But beyond the traditional concept, GO developers believe that in the programming process, in order to ensure the robustness and stability of the program, the precise handling of exceptions is very important. Only after each logic processing is completed, clearly inform the upper layer call whether there is an exception, and the upper layer calls to handle the exception clearly and timely, so that a high degree of robustness and stability of the program can be guaranteed. While this will lead to a lot of judgment on error results in the programming process, it will undoubtedly increase the developer's vigilance to exception handling. Practice has proved that it is difficult to write unrobust code as long as you code strictly according to the style recommended by GO. Of course, as long as you don't reject it and recognize it.
Supporting multiple return values for functions in the language is nothing new, and Python is one of them. Functions are allowed to return multiple values, which can effectively simplify programming in some scenarios. The programming style recommended by the GO language is that the last parameter returned by the function is of type error (as long as an exception may occur in the logical body), so it is necessary to support multiple return values at the language level.
Defer delay processing mechanism
In the go language, the keyword defer is provided, through which you can specify the logical body that needs to be delayed, that is, before the function body return or when panic appears. This mechanism is very suitable for follow-up logical processing, such as avoiding possible resource leakage problems as soon as possible.
It can be said that defer is another very important and practical language feature after goroutine and channel. The introduction of defer can simplify programming to a great extent, and appear more natural in language description, greatly enhancing the readability of the code.
As a strongly typed compiled language, Golang is naturally not as flexible as a parsed language. Such as PHP, weakly typed, and you can directly new the contents of a string variable, which is obviously not possible in compiled languages. However, Golang provides Any typing (interface {}) and powerful type reflection (reflect) capabilities, and the flexibility of development is very close to that of a parsed language. In terms of dynamic invocation of logic, it is still very simple to implement. In that case, what is the advantage of a parsed language like PHP over GO? Personally, I have written PHP for nearly 10 years, and have implemented the development framework, basic class libraries and various common components. Although the implementation performance is insufficient, the development efficiency is more than high. When it comes to Golang, these advantages do not seem so obvious.
As a server-side language in the Internet era, the ability of user-oriented service is essential. GO has its own HTTP/TCP/UDP high-performance server at the language level, which provides the most direct and effective capability support for business development based on co-program concurrency. It takes only a few lines of code to implement a high-performance HTTP Server in the go language, which is very simple.
In the go language, there is a set of standard project management specifications, as long as the project development according to this specification, after things (such as package management, compilation, etc.) will become very simple.
Under the GO project, there are two key directories, one is the src directory, which holds all .go source files, and the other is the bin directory, which is used to store compiled binaries. Under the src directory, except for the directory where the main main package is located, all the directory names correspond to the corresponding package names under the direct directory, otherwise the compilation will not pass. In this way, the GO compiler can derive the engineering structure and build order entirely from the directory structure and package name, starting from the directory where the main package is located, avoiding introducing an extra Makefile file like C++.
During the compilation of GO, the only thing we need to do is to assign the GO project path to an environment variable called GOPATH to let the compiler know the location of the GO project that will be compiled. Then go to the bin directory and execute go build {the directory name where the main package is located} to complete the compilation of the project in seconds. The compiled binaries can be pushed to the same kind of OS and run directly without any environment dependence.
The programming specification of the GO language is forced to be integrated into the language, such as clearly specifying the location of curly braces, forcing one sentence per line, not allowing the import of unused packages, not allowing the definition of unused variables, providing gofmt tools to force formatting code, and so on. Oddly enough, these have also aroused the dissatisfaction of many programmers. Some people have published the XX charges of the GO language, including accusations against the programming specification. You know, from a project management perspective, any development team will develop specific programming specifications for a particular language, especially for companies like Google. The designers of GO believe that instead of writing specifications in documentation, it is better to force integration into the language, which is more direct and makes more use of teamwork and project management.
Practice of API Rapid Development Framework
A programming language is a tool that tells us what we can do and how it would be better, which is also worth exploring. This section introduces a development framework implemented in the go language, as well as several common components. Of course, frameworks and common components can also be implemented in other languages, but the focus here is on cost. In addition, apart from the GO language itself, we also hope that we can get some ideas to solve the problem from the several components introduced, that is, to solve a superficial problem in some way, rather than blindly writing code, but only to solve the point problem in the end. If you agree with this approach, I believe that the following content may affect the way you develop the project in the future, and fundamentally improve the efficiency of development.
Why did we choose GO?
The choice of GO language is mainly based on two considerations
The execution performance shortens the response time of API and solves the problem of batch request access timeout. In the business scenario of Uwork, a batch API request often involves multiple calls to other interface services, but in the previous PHP implementation mode, it is very difficult to make parallel calls, but serial processing can not fundamentally improve the processing performance. While the GO language is different, the parallel processing of API can be easily realized through the cooperative program, and the processing efficiency can be maximized. Rely on Golang's high-performance HTTP Server to enhance the system's throughput capacity, from hundreds of PHP levels to thousands of miles or even more than ten thousand. The development efficiency GO language is easy to use, the code description efficiency is high, the coding standard is unified, and the start-up is fast. With a small amount of code, the framework can be standardized and API business logic can be quickly built with a unified specification. It can quickly build a variety of common components and common class libraries, further improve the development efficiency, and achieve mass production of functions in specific scenarios.
When many people learn a new language or start a new project, they are used to finding an open source framework that they think is appropriate to start their own project development journey. There's nothing wrong with this, but personally, it would be more helpful for us to understand its internal implementation. As you may have noticed, the essence of the MVC framework is to parse the request path, and then route it to the corresponding controller (C) according to the request path segment, and then the controller further invokes the data logic (M). After getting the data, the view (V) is rendered and returned to the user. In the whole process, the core point lies in the dynamic invocation of logic.
However, the implementation of the API framework is simpler than the implementation of the WEB page framework, because it does not involve view rendering, but only needs to return the data results to the user in a protocol manner.
Using go language to achieve a complete MVC development framework is very easy, while integrating HTTP Server, the core code of the framework will not exceed 300 lines, from here you can actually feel the high efficiency of GO language description (if you are interested, you can refer to the Uwork open source project seine).
Some people also say that in the go language, there is no framework to speak of. The implication is that it is not necessary to introduce a heavy open source framework, on the contrary, it may complicate simple things.
In the actual project development process, only efficient development language is not enough, in order to further expand the development efficiency, it is essential to constantly precipitate the common basic library, in order to further abstract and reuse the general basic logic.
In addition, the ability of general components is the foundation to achieve functional mass production, and it will be a qualitative improvement to the development efficiency. The component-based development model will help us to improve our problem-solving ability from one point to another. The following will focus on the implementation of several common components, with their existence, can really liberate the productivity of programmers. However, the implementation of these powerful common components in Golang is not complicated. At the same time, combined with the concurrent processing ability of Golang, compared with the implementation of PHP version, the execution efficiency will also be qualitatively improved. This is a perfect combination of component capabilities and language efficiency.
The common list component is used in the data query scenario of all possible two-dimensional data sources (such as MySQL/MongoDB/ES, etc.), which solves the data query problem from one aspect. In Uwork project development, it is widely used to realize the mass production development of data query interface and page query list. It takes a JSON configuration file as the center to query the general data source, and automatically returns the query results to the user in the form of API or page. There is almost no code development in the whole process, and the only thing to do is to write configuration files (rather than code) according to a unified specification, which really realizes the mass production of data query requirements.
The above is the process of building general list components. Does it feel out of reach to implement such a powerful general component? In fact, this is not the case, as long as the whole process is sorted out and the building ideas are integrated into the Golang, it is not a complicated thing. In our project, the implementation of the whole component solved a series of data query problems with less than 700 lines of Go code. In addition, through the concurrency feature of Golang, the parallel execution of the field processor is realized, which further improves the execution efficiency of the component. It can be said that the integration of general list and Golang is the perfect combination of performance and efficiency.
The general form component is mainly used to add, delete and change the scene of the database. This component is also widely used in the project development of Uwork. Similar to the general list, it takes a JSON configuration file as the center to complete the operation of adding, deleting and changing the data of the data table. In particular, the recently completed component-level SDB management platform realizes the data maintenance of the whole system through the general form, and achieves the code-free production of the business through high abstraction.
The above is the complete construction process of the general form, but for the implementation of this component, we use less than 1000 lines of GO code to solve the whole problem of maintaining the data of the datasheet.
GO language itself supports co-programming concurrency, which is very lightweight and can quickly start thousands of collaborative work units. If the number of collaborative tasks is not properly controlled, the final result is likely to be counterproductive, thus putting unnecessary pressure on external or own services. The cooperative pool can control the number of execution units to a certain extent and ensure the security of execution. In Golang, it is very simple to implement such a pool, it only needs to encapsulate channel and goroutine, and the whole build process is less than 80 lines of code.
In the process of API development, data validation is always an indispensable part. If it is only a simple data check, a few lines of code may be completed, but when it comes to complex data validation, it is likely that hundreds of lines of code may not be completed, especially when it comes to recursive type of data validation. it's a nightmare.
The data verification component can complete the general verification through a way of configuring the data template and using specific logic. Developers only need to configure the corresponding data template and make a simple call to complete the whole verification process. For such a universal data verification component, the whole construction is completed with less than 700 lines of code in the go language.
Summary
In the process of actual project development, the greatest improvement in development efficiency is undoubtedly the common component capability in line with the system business scenario, which coincides with the sentence of Rob Pike (Less is lessor Less is more). The real efficient development is configured, and it does not need to write too much code, or even need to write code at all, to complete the logical implementation, and this way is also the best for later maintenance costs. Because a high degree of unity has been achieved.
There is no doubt about the language description efficiency of GO, and the implementation of all the above common components is no more than 1000 lines of code, which solves some problem.
(some of the above code has been provided in the Uwork open source project seine.)
Performance evaluation
Stress test environment description:
Service running machine: a single free B6J 24-core CPU with 64GB of memory. PHP API environment: Nginx+PHP-FPM,CI framework. Nginx starts 10 child processes, each receiving a maximum of 1024 connections, and php-fpm uses static mode to start 2000 resident child processes. Golang API environment: compile with go1.8.6, directly pull up the Golang API Server process (HttpServer), regardless of tuning. Customer-initiated request test program: written in Golang, concurrent, running on another independent free B6, 24-core CPU,64G memory, 20000 requests at 1-2000 different levels (concurrency step size 50) respectively.
Comparison of stress test results
In the Golang API framework, the processing QPS fluctuates near the 6.5w/s when the number of concurrency is more than 50. The performance is stable and there is no error report in the stress test process.
Nginx+php-fpm, which only outputs exit ('ok') in index.php. When the number of concurrency is more than 50, the processing QPS fluctuates near the 1w/s. The performance is stable and there is no error report in the stress test process.
In the Nginx+php-fpm+CI framework, the logic executes to a specific business logic point and outputs exit ('ok'). When the number of concurrency is more than 50, the processing QPS fluctuates around 750 Universe. And the performance is unstable, and the number of errors increases with the increase of the number of concurrency in the process of stress testing.
Through the stress test, it can be found that there is no comparison between Golang and PHP in terms of execution performance, while the HTTP API framework implemented by Golang has a stand-alone performance QPS of 6.5w/s under no load, which is very satisfactory.
Points that should be paid attention to in the process of development
The following are some problems encountered in the actual development process, for reference only:
Exception handling uniformly uses error, do not use panic/recover to simulate throw. Catch, I did this at first, but later found out that it was completely self-righteous. The native error is too simple, but in the actual API development process, different exceptions need to be accompanied by different return codes. Based on this, it is necessary to encapsulate error. For any cooperative logic executor, there must be defer recover () exception recovery handling at the beginning of the logic, otherwise the panic in the goroutine will lead to the downtime of the whole process and it is necessary to avoid the global impact caused by part of the logical BUG. In Golang, the operation of variables (except chan types) is not thread-safe, but also includes basic types such as int, so it is important to consider locking when concurrently manipulating global variables, especially on map. All the acquisition of map keys should be judged on their existence, and it is best to encapsulate similar operations uniformly to avoid unnecessary runtime exceptions. When defining slice data types, try to preset the length to avoid unnecessary data reorganization within.
After reading the above, do you understand why PHP programmers want to learn the GO language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.