Golang vs PHP

Source: Internet
Author: User
Tags autoloader php language hosting vps
This is a creation in Article, where the information may have evolved or changed.

The Go programming language is still new and growing in popularity. I ' ve been playing with it for a few months, learning by writing a blog engine. I ' ve enjoyed it, and when I realized I had the basic blog features I needed, I tossed out my old PHP based blog and switch Ed to using Go full time.

When I started to write this post I thought I would has a couple of little differences in Golang vs PHP. As I wrote, I realized that they is very different. At their core they perform logic, and manipulate the input to create output. But they take very different paths to get the that output.

Go is simple and small. There is not a lot of keywords or functions to remember. Everything is tidy and thought out with specific intent before being included in Go. It feels easy-to-use. The syntax is similar-to-PHP so I-felt at home. The build environment and online documentation made it easy for me to get working code up quickly.

PHP, on the other hand, is huge. There is years of organically grown functions. Many that does the same or similar things, with similar names to keep it confusing. PHP is easy-to-use because it ' s direct. It doesn ' t require a lot of the knowledge or setup to quickly create something useful. But as your program grows it's complexity and maintainability grow with it.

Static vs Dynamic types

The first difference you'll notice deals with how variables is managed. Go is statically typed and PHP are dynamically typed. In PHP A variable can is of any kind. It could is a number (integer or floating point), String, Boolean, object, or array. If you had a variable that is originally set to a number you can later assign text to it. If you had a variable that's a text zero can pass it functions that's looking for an integer or Boolean. PHP would do it's best-to-convert between types depending on what it needs.

Go is the opposite of this. Once A variable is declared as a specific type it can only be ever contain data of that type.

When I wrote PHP I made a point to create the single use variables that were effectively statically typed. I would cast variables when I knew a type cast is needed. I did this to keep my code simple and has greater control over type conversions. Because of this my transition to Go ' s statically typed variables is easy.

Objects and inheritance

Go does not has objects like PHP does. It has structs, the basic data Structures which act like PHP objects and that does not have methods. But structs can has methods attached to them later. This could seem like a object oriented nature but it's rather different. To me it seems like Go does objects inside out. The language designer want you think about building programs through composition. You is encouraged to make small piece of code and pulling them together to do large things.

"If something can do this and then it can be used here."

Effective Go

Go uses implied interfaces for code reuse. Interface type declarations is a set of methods. As long as your type have at least those methods it fulfills that interface. And would work wherever this interface is used.

Program Structure

There is many many ways to write PHP programs. The first of the and the most popular are old school PHP mixed in with HTML. Each page was a stand alone program, or at the least includes a "base" or "functions.php" from a global location. The second is strict separation of PHP classes and functionality organized into a semi-standardized folder structure. This method uses a "autoloader" to find and load PHP code when it's needed by the request.

For my Go blog engine I used a structure closer to the second. I have a project directory with source, binary, static root (HTML, CSS, JS), and Go templates. The Go source and dependencies live in the SRC and pkg directories. When compiled the binary lives in bin. The HTML, CSS and JavaScript that is separate from the program logic is stored in the Web root. The only publicly accessible folder. My named Templates live in the Templates directory.

Hosting Environment

PHP runs on commodity hosting environments amazingly well. It has a simple understanding of what a. A PHP file in the Web root was requested, compiled and the output sent to the browser. Go is not as straight forward to setup. It designed for big software on servers, that's you control. This offers greater control at the expense of a hosting complexity.

There is many Golang hosting solutions, I personally run my own server on a VPS. I use Apache for several sites and found a from integrate my Go program into my existing setup. The go binary listens on localhost, and Apache forwards requests for nonexistent files or directories to the go binary.

I ' m not sure how easy it would being for a commodity the host to setup a Go environment that's as simple to use as a PHP setup. Google does offer Go on it ' s AppEngine platform. My recommendation is to use a VPS and manage the servers yourself.

Compiled vs Run-time

PHP, in it's simplest form, is dynamically compiled on the each server request. The code lives in the Web root and are compiled fresh each time a browser hits the page. Go on the other hand are compiled once into a static binary. The program was compiled one time, and pulls in all the program's dependencies, to create one single binary file. This executable file, in the case of a web app, was pointed to from your Web server. Any request this need to being routed to the Web app is sent to the binary, executed and the result returned to the browser.

This would affect your workflow. With PHP you can simply make small edits and test them in a browser with a simple refresh. Go has a extra build step, where you must compile and restart the binary before you refresh the your browser. Go is very fast when it comes to compile times. So fast is the time I switch to my browser and refresh the recompile and restart is complete. The speed of testing hasn ' t hindered me. Still, I find my workflow changing in subtle ways. Now I take the more time to implement changes and updates to my blog engine before testing. Whereas with PHP I would test sooner on smaller edits.

Package Management

Go has a built-in tool to get and install the third party libraries. Would go get http://path.to.source.code download the source and put it in the PKG directory, ready for use. Until recently with Composer PHP do not has a native package management tool. All PHP dependencies had to is manually copied into the right location for the program to use. Go ' s package management tool was direct and simple compared to Composer.

Go is very strict about how it uses packages of external libraries. PHP would include () any code it was told to. Or try to load any code sent to the Autoloader (). PHP litter your source with unused variables and functions. Go won't.

Go to complain very loudly by not compiling. At first you'll be frustrated by this, but it'll pass. My programming style adapted to this strictness. Which is a whimsical desire of the Go team. It keeps your code clean. I know that if my code compiles then every last piece of it is used somewhere. There is no ambiguity if a function is a unneeded.

Code Format:go FMT vs random style

The great syntax formatting Wars is coming to an end. Go includes a tool called go fmt which you can use to automatically format your source files. In this Go is opinionated. It uses tabs not spaces for indentation.

The syntax style very strict as well. If statements don ' t use parentheses around the expression block, and you must use curly braces around the statement block. No single line if statements anymore.

Templating:go ' s crystalline structures vs PHP ' s spaghetti

One of the most dramatic differences for me is in how Go implemented it's templating system for outputting HTML pages.

PHP uses the core PHP language as it ' s templating system. It inter-mingles HTML and PHP code. The HTML is sent as was to the browser, and the PHP are processed and the output sent to the browser. It is a wonderful system for working with small PHP scripts. It's simple and allows for the whole of the script to exist in a single page. But it breaks to your program grows. It limits code reuse and creates confusing boundaries between code parts. It creates spaghetti code.

Go uses a simpler templating system. One that I am still getting used to. It uses an external templating syntax that's pulled into Go and processed. You create the name template parts and pull them into each other to the create full pages. You then load the top named template to Go and pass your data into it.

The Go template itself has little logic and as far as I can tell. You had the option to test for the existence of a named variable, and not what it was. So you can ' t compare if a value is equal to something, or is larger than some number. At least I has yet to find it.

For more complex needs you can create custom filters. These is functions that is run from the template on the data. So you can add them to specific parts of your template. I created a filter, checks for a tag in a blog post and stop rendering if it finds one. This allows to post excerpts on pages and that has multiple posts; Like the homepage.

Like the rest of Go the Spartan nature of the templating language is purposeful. Instead of creating quick fixes here is the one template, you have to adjust the data sent to the template. You is forced to fix the issue in the appropriate place, the business logic.

The End

These is a comparison of the most apparent differences between Go and PHP. Go has the other features is not available in PHP, such as channels and goroutines for concurrency. But their differences is deeper than the syntax and available libraries. Go has a philosophy of small and simple. It feels easy and direct. In it ' s strict nature it feels liberating.

PHP programmers who is used to fast and loose would feel frustrated and hobbled by Go ' s strictness. But if you were like me, and desire a simplicity of nature and directness of design, then you'll enjoy working with Go.

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.