Go, why it’s the best thing for web development and the replacement for PHP
I have a new favorite language in Go, Google’s not-so-new language that was badly advertised when it was first released in March 2012. It had the structure and style of a C or Algol with the speed of a near C or C++, while having features of Java and Python, and there are no classes in it in the traditional sense. All of these varying characteristics seem to be the perfect recipe for a smelly dump, but it actually is amazingly simplistic while maintaining the needs that would be associated with a Java or Python. However, I feel that Go needs to replace PHP. Now there are millions of lines of PHP code that will take a very very long time if ever to be replaced, and make no mistake, PHP is not dead. Not by a long shot. It’s on version 7.1 and it will continue on with new iterations or with a deviation like Hack and HHVM developed by Facebook. However, Go can replace it for a multitude of reasons:
- Speed is a big thing now in the web. It’s all about speed now. According to FastCompany, “Amazon’s calculated that a page load slowdown of just one second could cost it $1.6 billion in sales each year. Google has calculated that by slowing its search results by just four tenths of a second they could lose 8 million searches per day–meaning they’d serve up many millions fewer online adverts.” According to this benchmark, serving up plain webpages means Go speeds ahead of the other guys at 27K requests per second, including my old favorite PHP framework Lumen at just over 800 requests per second. That’s a speedup of over 30x.
- Conciseness is a huge thing for developers. It’s one of the reasons why many developers prefer a language like Python over Java: Python can in most cases do the same amount of work in less lines of code. Less lines of code means less time spent developing on the software, which means more time for features or more time doing more fun stuff. And, conciseness is a huge advantage for Go over PHP. So far my Go code for a project that I am converting to Go from PHP has 18 files to deal with. Just the model files alone in PHP are 18 files. With PHP, it’s easy to go quickly into dealing with over 100 files for a simple project that you have to deal with directly, and thousands of files that you have to indirectly deal with.
- Built-in libraries provide most of the functionality needed. Pretty urls? Got it. MVC functionality? Also has it. Simple templating? Built-in. It makes life so simple that most simplistic functionality is already built-in without having to worry about libraries and building your library dependency list with libraries that might or might not be updated and also bloating your code unnecessarily. Also, get many libraries put together and you decide that getting a framework might be the best of options which slows down your PHP code even more. AWS and Google Cloud SDKs both exist for Go. There are not any requests by libraries that you use to get even more extensions for PHP which hurts your setup a server time and script even more.
- Security is much simpler in Go than in PHP. Of course, you do need an external library or two for some features such as password hashing that are built-in into PHP, and there are going to be some features that have not yet been implemented to the satisfaction of developers, but Go is still a work in progress. Languages take years to develop and mature into something that is well developed.
- Compilation than interpretation. Compiling the Go code into machine code is what ensures its speed compared to PHP. Reading machine code is always faster than reading code that has to be read or even JIT compiled like in HHVM. And, it doesn’t run as quickly into a memory bottleneck like Java does because there is no virtual machine to support. PHP, Python, and many of these other interpreted languages were developed at a time when Moore’s law meant that performance had significant gains each and every year or two years if not one year. There was a thought that performance gains would effectively make these performance differences negligible. What has happened instead is that users have demanded more performance than before; webpages need to load faster, data needs to be processed quicker, etc. Compilation to machine code is the best way to make sure that it is at the fastest possible speed.
- Maturity is much better than Swift. The effects of Swift 3 is still reverberating throughout the iOS and Apple community. Apple seems intent on breaking the previous version when they update Swift in a major way. Go, even though it has been updated every six months, has maintained (for the most part) compatibility with previous versions, ever since Go 1 in 2012 (which was five years ago when posting this article). It might not have the feature parity or library parity of a Java or PHP or Python, but I would argue it is at least 70–80% of the way there if not larger. Swift 3 broke every library, every app, and required major changes to each and every app, and Swift 4 promises to do the same thing. With statistics from Git proudly saying “114 files changed, 3278 insertions(+), 3086 deletions(-)” that’s not exactly something I want to repeat across my entire codebase. And, that only includes the changes I had to make to get it to run, not the changes that made it run without any bugs or feature issues (I had to delete some lines of code in order to get it to build). Go does not have any of these issues.
For all of the reasons stated above and some that are not, Go seems to be the new developer’s choice for a new web project. The fact that most of the features needed is built into the language itself and libraries are very light and build on the built-in language serve to exemplify why Go is the future. Unlike PHP, where a framework is recommended if not required for a multitude of reasons, and that framework classes can have many parent classes easily (check out Laravel), that can complicate and add more bugs, more dependencies, more space, more of everything to worry about. With Go having the feature parity of a PHP framework while having the simplicity of a regular language, it’s easy to see why Go is the next generation language for web development.