Why do you PHP guys should learn Golang

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

Go or Golang, is a open source, community supported, fast, consistent, scalable, productive language, and conceived by Goo Gle. Applications were build with it. Although Rob Pike had said that "... we expected C + + programmers to see Go as a alternative ...", but I really think of that: You PHP guys should learn golang! Let's talk on why.

Easy to learn

PHP is easy to learn. Golang is as easy as php!

The syntax of PHP is similar to C family ' s programming languages (C/c++/java etc). If you had some experiences with those languages, it was not too hard-write some codes when you first met PHP.

Golang is also a C family ' s programming language. Eh ... with a litte syntax difference. For example, the keyword "for" was similar with PHP's, but without the pair of parentheses. and the condition keyword "if" is without the pair of parentheses either. You can read effective Go to more things.

Golang have only keywords and operators, delimiters or other special tokens. There is no need to work hard for remembering all the literals. The type system is exquisite. No huge OO But a practical struct type with methods on it. The interface design is one of my favorate parts of Golang. When you had finished this A tour of the Go, based on your PHP experiences, some jobs could was done with Golang immediately.

Easy-to-use

Your PHP scripts would be evaluated by SAPI components:web Server module, PHP-FPM or CLI. All needed to PHP deploying is a SAPI environment. Deployment of the environment is the most difficult part to understand for a PHP beginner.

All the Golang ' s codes is compiled and linked into native code. So there are no need to deploy a environment for it executing but a build environment. Compaire with deployment of the PHP environment, it's not so much complex. Do you really think of the deployment of the PHP environment are really complex? I don ' t think so. really!

To deploy the build environment, just need 3 steps:

    • Download Golang ' s source code.
    • Setup the environment variables. Here's a great blog Post:go environment setup
    • Run the ' All.bash ' in the ' src ' directory.

Or only one step:use the binary package to setup.

Then a toolset called "Go" can is used. The Using the Go tool is an as easy as using the PHP ' s CLI tool. "The Go Tool" has already explained it.

A Myth of PHP

If A programming language is easy to learn and use, shall we learn and use it? Lots of programming languages is easy-to-learning and use. Shall we learn and use all of them? The answer is straightforward: no!

But why do you PHP guys should learn Golang? Just because it is cool! Ja, I ' m kidding, but it is true. Anyway, let's talk about PHP itself first.

PHP is ' a general-purpose server-side scripting language originally designed for web development to produce dynamic WEB Pages. (Wikipedia) ". The most important feature of PHP are embedding with HTML. PHP code write in "<?php ...?" > "tag; HTML write outside. It also has a powerful extension system. The extension can is writen by C with Zend API. Data processing was actually do by those extensions. IMO, PHP must is the best template programming language.

But if you had years experiences with PHP and began face to some complex web application. You must has already felt some powerlessness to PHP. It Dosen ' t has concurrent mechanism build-in, no threads, multiprocessing or x-routines. A slow data source would be blocked entire page ' s processing. A powerful Web server in the front of PHP are always needed. Backend programs were writen to process data. Message Queue, Cache, Proxy ... The system is not only on PHP, but also about a lot of services and system components. This time, PHP was really be-a template language with few logical jobs.

Phpers is always looking for solutions, like "PHP multithread" or a concurrent the RPC framework for PHP. I could not say witch which one is better. But I ' m sure so you must choose a other programming language for the backend job. In my own case, I tried C (struggle with Malloc/free)/java (stumble in the Jar Hell)/python (Never is pythonic but type lost) .. If I get profermance, I must struggle with the memory management; If I use GC, I must deploy and turning VM; if I get convenient, careless would kill me....eche have advantages, each also have A problem.

Ok! Let's back to Golang now!

Golang has GC, there are no need to care about memory management (or could pay less attention to it). And the code is compiled into native machine code. So, "CP" and "MV" is all needed for deploying the application writen by Golang. Oh, as I just said, Golang is a compiled language with a static type system. You have no chance to confuse variable types. Of course, one of the most important thing are phper who moves to Golang "don ' t has to surrender much expressiveness, but Gain performance and get to play with concurrency (by Rob Pike) ". "Why isn't Go?" talked about this deeply.

I would show parts of my own experience:a Gearman worker works for us to process backend data. PHP connected to the worker though Gearman Job Server with its Gearman API. The original worker is writen by Python. There is lots of problems with that version (they is our own problems, but Python's). But it works for us. Then we rewrite the worker with Golang. It was amazing. I wrote a Gearman API for Golang, and a Golang package using Zend API (CGO are another amazing thing) for evaluating the PHP Script in Golang. Then combined them together–a Gearman worker Witch which can execute PHP scripts. It works about 3 Mounth in our production enverionment and looks great! Oh, I also wrote a rpc-combinator for combining the RPC call in PHP script. It'll be used in our next product.

Everything goes right. Thanks golang!

In any case, most of phpers alwasy need to learn and other languages for the backend development. If you had began looking for, or already tasted some of other languages. Why don ' t try Golang? It would make your life happy and easy. You could spend more time with your family and friends, eat your favorite foods and visit the places your love.

That's why you PHP guys should learn golang! Is it enough? I Think So!

This is my writing exercise. If you find any grammatical or spelling error, plz tell me!

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.