Why you PHP guys should learn Golang

Go or Golang, is a open source, community supported, fast, consistent, scalable, productive language, and conceived by Google. More and more applications were build with it. Although Rob Pike has said that “… we expected C++ programmers to see Go as an alternative …”, but I really think of that: You PHP guys should learn Golang! Let’s talk about 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 have some experiences with those languages, it is not too hard to 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” is 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 for more things.

Golang has only 30 25 keywords and 47 operators, delimiters or other special tokens. There is no need to work hard for remembering all the literals. The type system is exquisite and easy to use. 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 have finished this A Tour of Go, based on your PHP experiences, some jobs could be done with Golang immediately.

Easy to Use

Your PHP scripts will be evaluated by SAPI components: web server module, PHP-fpm or CLI. All needed for 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 will be compiled and linked into native code. So there is no need to deploy a environment for it executing but a build environment. Compaire with deployment of the PHP environment, it is not so much complex. Do you really think of the deployment of the PHP environment is 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 be used. Using the go tool is as easy as using 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 are easy to learning and use. Shall we learn and use all of them? The answer is straightforward: NO!

But why 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 is embedding with HTML. PHP code write in “<?php … ?>” tag; HTML write outside. It also has a powerful extension system. The extension can be writen by C with Zend API. Data processing was actually done by those extensions. IMO, PHP must be the best template programming language.

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

PHPers are always looking for solutions, like “PHP multithread” or a concurrent RPC framework for PHP. I could not say witch which one is better. But I’m sure that you must choose an 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 be 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 has advantages, each also has a problem.

OK! Let’s back to Golang now!

Golang has GC, there is no need to care about memory management(or could pay less attention to it). And the code are compiled into native machine code. So, “cp” and “mv” are 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 is PHPer who moves to Golang “don’t have to surrender much expressiveness, but gain performance and get to play with concurrency(by Rob Pike)”. “Why Not 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 was writen by python. There are lots of problems with that version(They are 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 is 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 will be used in our next product.

Everything goes right. Thanks Golang!

In any case, most of PHPers alwasy need to learn other languages for the backend development. If you have began looking for, or already tasted some of other languages. Why don’t you 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 you 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!

Comments

24 responses to “Why you PHP guys should learn Golang”

  1. Dustin Nielson Avatar
    Dustin Nielson

    If I was interested in checking into this what hosting companies support this? One of the reasons I’m a PHP fan is that you can pretty much find support for this anywhere regardless of platform.

  2. mikespook Avatar

    Have you tried GAE?

  3. thirsteh Avatar
    thirsteh

    Heroku supports Go, so does any shared hosting provider that lets you have shell access, since Go applications are just a single, statically-linked Linux/Windows/etc. binary.

  4. jack2 Avatar
    jack2

    Great article, maybe I suggest another topic for your next rant?

    Why you China guys should learn English

  5. Mat Avatar
    Mat

    Let’s be realistic, most PHP devs I know come from a C++ and Java background. So when it comes to finding a job, they don’t need to learn a new language. Also, why would they want to learn Go when some of them are already using other cool languages like Python and Ruby? I have absolutely no idea. You should target JavaScript devs, they believe all this nonsense.

  6. ruby flew too Avatar

    Good article – I’m inspired to give Go a go 🙂

  7. Doe Avatar
    Doe

    @Mat PHP dev coming from a Java background? Sorry, I don’t know any experienced Java developer who switched to PHP.

  8. Nicolas Avatar
    Nicolas

    Good article. Some typos. 4th paragraph after “A myth of PHP” : “Backend programes” -> “Backend programs”. 2 par. below that “veriable types” -> “variable types”.

  9. mikespook Avatar

    thx @Nicolas!

  10. itoctopus Avatar

    Hi Mike,

    You’re saying that more and more applications are written in Golang. Can you list a few here? Most of us here don’t want to use a language that may or may not survive because of lack of adoption.

  11. Greg Avatar

    There are no ads on Dice for Go developers. There are 5,600+ for Java developers, 3400+ for .Net developers, 700+ for PHP, 300+ for Python, but NONE specifically asking for Go developers in the subject of the job posting.

  12. Lon Avatar
    Lon

    I appreciated seeing this article because I’ve been a PHP guy for a decade and I just switched to Go and I couldn’t be anymore happier. Thanks for spreading the word.

  13. Ronald Avatar
    Ronald

    Can you develop in Golang under a windows environment?

  14. Phil Avatar

    I think its pretty silly to start listing job counts on languages as a reason to avoid learning it. How many jobs were listed for Java when it first came out? Objective-C? Just a few short years ago a PHP dev couldn’t get a “serious” job because it wasn’t accepted in enterprise. Now with economic changes and other factors companies are trying to use PHP at every corner. I’m getting a ton of PHP calls now one of which was a contract that I accepted at a Fortune 500. But ironically just this week in the middle of all the PHP work going on here Go saved our butts because PHP would have been too slow for the job we needed to handle. Luckily I knew the language. It was quicker to write what we needed than using C or C++, faster than all of the scripting languages in execution, didn’t require any external environment like the scripting languages or Java (we couldn’t put one on the prod box anyway) and compiled into one nice native executable. My manager needed a 64 bit version so I pointed him to the download for the SDK and within minutes he compiled the 64 bit version and had never heard of Go before that day.

    So folks learn Go. Learn lots of stuff. Keep your tool set broad and don’t just learn languages with high job counts because you never know when a language is going to step into prime time. Its not the only language a PHP dev should learn BUT it can make a lot of sense to do so especially if you aren’t just looking for jobs but also building your own potential startup projects. Its not hard to pick up and gets you better performance. And again its native so anywhere you can run an arbitrary executable you can use it. I just built a service on OpenShift with it. All the environment and runtime concern goes out the window.

  15. Phil Avatar

    @itoctopus

    http://go-lang.cat-v.org/organizations-using-go

    It seems like fairly good uptake for a language so young but it makes sense. Reuse your current devs that know languages like Python and Ruby but get systems that run much faster. When you have companies switching their backends to Go that’s a sign that it may have some staying power.

  16. Luis Tineo Avatar

    Great article… I really this is what we need to do with every programming language. After all they are tools. If you are a religiously addicted fan to a programming language posts like this will be profanity… so great work 😀
    I think Golang is awesome but it is in my bottom 10 of cool languages to use, learn or implement. Scala, Haskel, Erlag, Vala and IO language are in the top 5.

  17. […] 好吧,说了一大堆,回归主题。之前我写了一篇英文练笔《Why you PHP guys should learn Golang》,获得不少国际友人的关注。排除拼写和语法被他们诟病外,主要是有许多朋友觉得我没把事情说清楚。所以这里我用母语重新聊聊这个事情,只是这些国际友人什么时候能学会阅读中文呢?;) […]

  18. M. Yitzhak Samuel Avatar
    M. Yitzhak Samuel

    It’s been a problem for a long time. Perl has similar issues. The environment that php runs in can be radically different between servers. Even if you think you’re running the same version of the same modules, you could be tripped up by a bad build number, or pecl not agreeing with whatever it was that yum or aptitude installed. PHP is a great language when you’re working in a homogeneous predictable environment.

    It’s absolute hell when you start talking about distributing your code. It can be done. You can code scenarios for every possible server configuration, if you want to. Or you can code an install program that parses through the configuration and figures out what you need, but why? Why are you going through the effort of it when something like Go is available?

    Sure, Go is quirky. Really quirky. It requires a different mindset than php, and it takes more than a weekend to build proficiency in it. If php was your first serious language, I’m not going to lie. There is a learning curve there. But it’s not severe.

    What makes Go compelling for me, as someone interested in distributing software is that it has none of the problems php has. It’s fast, it’s easy to set up, and you get a level of control with it that simply isn’t possible with php scripting. Control is a big factor for me. I’m a self hating control freak, so it’s important.

    Anyway, it’s great for packaging and selling software. One file, hit it at the command line, maybe some simple server setup if you do cgi mode, and you’re set up.

    For browser based stuff, it makes a lot of sense.

  19. M. Yitzhak Samuel Avatar
    M. Yitzhak Samuel

    In all honesty, it doesn’t sound you like have much of a grasp of php, with some of the blatant mischaracterizations in this article. Love Go though.

  20. mikespook Avatar

    @M. Yitzhak Samuel
    I have to admit, sometimes, our problems are not yours. But am I right or wrong totally? Could you point out the “the blatant mischaracterizationstions” in this article for me?

  21. bombless Avatar
    bombless

    witch -> which
    大家都没发现么(抠逼

  22. mikespook Avatar

    谢谢!已改正……看来,越低级的错误,越容易被忽略。

  23. […] この記事は、mikespookさんの2012/8/10の記事Why you PHP guys should learn Golangの非公式翻訳です。詳細はこの記事をご参照下さい。 […]

  24. kwmt Avatar

    Good ariticle!
    我是日语翻译了
    Because I think I want this content to spread to many japanese people.
    如果有问题,请联络。
    谢谢。

Leave a Reply

Your email address will not be published. Required fields are marked *