Why Phper should learn Golang

Source: Internet
Author: User
Tags php for beginners sapi learn golang
This is a creation in Article, where the information may have evolved or changed.

Familiar with my friends should know that most of the recent years my work will be much related to PHP. With more and more in-depth understanding of PHP, as well as more and more different business, the use of PHP always gives me a sense of powerlessness. Of course, I'm not alone when I'm having trouble using PHP. In fact, every little bit of experience, people who have been exposed to a few needs will have the same confusion. Various matching LAMP (or lnmp?) The backend technology of the architecture has been invented or discovered, and thus integrated into the technical system of PHP's development. From the simple memcached as data relay, cron backend timing processing, to Gearman, RabbitMQ these queue artifact; recently laruence even encapsulated Libcurl extensions that implement concurrent RPC calls with the asynchronous nature of Yar. Almost the entire community is looking for the path of Moses in PHP.

Well, say a lot and return to the subject. Before I wrote an English Lianbi "Why do you PHP guys should learn Golang", get a lot of attention from international friends. The exclusion of spelling and grammar was criticized by them, mainly because many friends felt that I had not made things clear. So I'm going to talk about this in my native language, but when will these international friends learn to read Chinese? ;)

Go or Golang is a fast, consistent, and stable open-source programming language supported by Google, with active community support. More and more applications are chosen to build using Golang. Although Rob Pike said "... We want C + + programmers to understand Go and as an optional language ... "but I really think:Phper should learn golang! then we'll talk about why.

Easy to learn

PHP is fairly easy to learn. Golang, too!

In this regard, a group of big foreigners have made a fierce attack on my views. They thought I humiliated Phper and said it was as if there were only simple things phper to learn. But isn't that the truth? Or, to put it another way: people like me who like PHP are more or less likely to prefer simple things.

PHP's syntax is close to the C-family programming language (C/c++/java, etc.). If you have experience with these languages, you can start writing code as soon as you encounter PHP for the first time. In my opinion, writing PHP code may be more of a test of a programmer's memory than intelligence (you will certainly agree with me when you are faced with a variety of different styles of function definitions and various extensions of special conventions).

Golang is also a C-family programming language. Uh, or something different. For example, the keyword "for", functionally and PHP close, but without parentheses. The conditional statement "If" also does not need parentheses. You can read effective Go for more information.

Golang has only 3,025 keywords and 47 action symbols, separators, or other special tags. Remembering these tags does not require any special effort. The ingenious type system is fairly easy to use. Practical, Method-structured types replace bulky object systems. The design of the interface is my favorite part of Golang. Once you've completed the Go guide, using PHP's experience, you can start using Golang to handle some simple tasks at once.

Easy to use

PHP scripts are parsed by the SAPI component, such as the WEB server module, the PHP-FPM, or the CLI. Everything you need to deploy PHP is a SAPI environment. Configuring this environment may be the most difficult part of learning PHP for beginners.

All Golang codes are compiled and linked to local codes. So in addition to compiling the environment, no special deployment is required for it to execute. Compared to the configuration of the PHP environment, this is much simpler. Do you really think it's complicated to configure the PHP environment? I don't think so, really! Configuring the Golang compilation environment is a bit simpler than that.

I'm sure there are a lot of golang related books and articles about how to configure the build environment. In order to be clearer, I am here to comb the idea.
There are three steps to deal with:

    • Download the Golang source code;
    • Set environment variables according to the "[Translation]go environment setting" prompt;
    • Run the All.bash in the source code src directory.

Or one step: Install using a binary package.

Then you get a collection of tools called "Go". Using the Go tool is as simple as using PHP's CLI tools. This is explained in detail in the [translation]go tool].

The Myth of PHP

If a programming language is easy to learn and use, should we learn it? There are many programming languages that are easy to learn and use. Do you want to learn all of them again? The answer is obvious:no!

But why should Phper learn Golang? Just because it's cool! Yes, I'm joking, but it's true. Anyway, let's start with PHP itself.

PHP was originally designed for the development of dynamic WEB pages with the server-side universal language (Wikipedia). An important feature of PHP is that it can be embedded in the HMTL. Code written in "<?php ...?" > "tag; HTML is written outside the label. It has a powerful extension system. The extension is written using the C call Zend API. Data processing is actually done using these extensions. In my opinion, PHP is the best template language in the world.

But when you accumulate some PHP experience and start to face some more complex Web applications, you're bound to have a sense of powerlessness in PHP. It has no built-in parallelism, no threads, no processes (do you really think that rudimentary process control can be used without modification in a highly concurrent production environment?) ), or some other "process". A slow data source can block the processing of the entire page. Message Queuing, caching, proxies ... The system began with more than just PHP, and included many services and system components. At this point, PHP only handles very little business logic and becomes the real template language.

Phper are always looking for solutions to this problem, such as "PHP multithread" or PHP RPC concurrency framework. I can hardly say which kind will be better. But I'm sure you'll need to choose some programming languages for the development of back-end work. In my own experience, I have tried C (always wrestling with Malloc/free)/java (Caught in the Jar Hell)/python (never did pythonic not say, and always in the wrong type) ... If you want to get performance, you have to wrestle with memory management, and if you use a GC, you have to deploy and tune the VM; when it comes to convenience, it's also a tip, a small mistake that causes a huge catastrophe ... Each has an advantage, and each has a problem.

All right! Now go back to golang!.

Golang has a GC that doesn't care about memory management (or can use less effort to focus on it). The code is compiled into a local code, so "CP" and "MV" are all the tools needed to deploy an application written by Golang. Oh, as I said earlier, Golang is a compiled language with a static type system. So you don't have a chance to mess up the type of variable. Of course, one of the important reasons Phper should learn Golang is "go to go because they don't give up too much expression, but get the performance and dance with the concurrent (Rob Pike)." This is an in-depth analysis of why not Go.

I can share some of my experience: there is a Gearman worker for processing back-end data. PHP uses its API to connect to Gearman's Job Server to initiate a request to the worker. The first worker was written in Python (and the more primitive version, PHP, but you can imagine it working ...). Alas, did not say ... )。 This version has a lot of problems (it's our own problem, not the Python thing), but at least it works. The worker was later rewritten with Golang. For this I developed the Golang Gearman API and wrote a package that executes PHP scripts in Golang using the Zend API. Then put them together: a Gearman worker who can execute PHP. It has been working for some time and looks good! Oh, inspired by Yar, there is also an RPC Consolidator written by Golang to incorporate RPC calls in PHP scripts. It's still a toy, but it might be useful in the future. This is actually using the Golang channel as a message queue. I have some explanations for this in "Golang: Interesting channel applications."

It's a beautiful world. Thanks, golang!.

In any case, most phper will need to learn some other language when it comes to backend development. If you are looking for, or have tried some other language. Why don't you try Golang? It's really going to make your life easier and happier. So you can spend more time with your family and friends, eat what you love, and go where you want to go.

Looks like I'm still not clear? Well, it doesn't matter, let's talk about this topic at the China Software Developers Conference next month.

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.