Home >Backend Development >PHP Tutorial >Jumping from PHP to Go: Blasphemy, Bravado or Common Sense?
Core points
This article about migration from PHP to Go was originally published elsewhere and was reproduced here with permission from the author.
Earlier this year, I made arguably bad business decision: I decided to rewrite the Laravel app that powered Boxzilla in Go.
However, I don't regret it.
After just a few weeks, I deployed the Go app. Building it was my happiest time in months, I learned a lot and ended up with a huge improvement over the old application: better performance, easier deployment, higher test coverage.
The application is a fairly simple database-driven API and account area where users can log in to download products, view invoices, or update their payment methods.
Stripe and Braintree are used to accept subscription payments. Invoices are processed using MoneyBird, and some transaction emails are sent using Mailgun.
While Laravel runs well enough, I always feel that some things are too complicated. And, what's going on with a new "main" version being released every few months? I would be happy if the newer version contains significant improvements, but many times, in my opinion, it just makes some minor changes to the name and directory structure.
Last year, I had moved several services to Go, so I wasn't completely new to the language. As a developer selling WordPress-based products, part of my job is to deal with an old technology stack that focuses on end users.
If I were not self-employed, I would apply for a new job to make up for this lack of “sexy” technology. As my own boss, it is my responsibility to make my daily work fun, not just to pursue more direct benefits. If income allows (and does), why not have some fun?
Writing Go code is a pleasant thing, with amazing tools, not only fast development, but the end result is usually incredibly fast. Just reading the purpose of the Go project made me fall in love with this language.
I think in the next few years we will see quite a few people moving from dynamically typed languages like PHP, Python, and JavaScript to Go.
Migrating code to Golang mainly involves doing database interactions correctly and porting Blade templates to something we can use in Go.
ORM always gets in the way of me, so I chose a simulateable data access layer and a normal SQL query. Meddler is used to eliminate some boilerplate code that scans query results to structures.
To support layered templates and partial templates, I open source greener, a small wrapper around Go's standard html/template package. This allows me to port the Blade template file to Go relatively easily, as I can use the same hierarchy and partial templates.
For integration with Stripe, there is the official stripe-go package. For Braintree, there is the unofficial braintree-go package that has been overlooked for some time but has recently received new attention. Since there is no Go package to manage invoices in Moneybird, I built and open sourced moneybird-go.
Since Go is a compiled language and its standard library is much better than PHP, it is not fair to compare the two languages that I'm about to do. That said, I think it would be fun to share some numbers.
wrk is used to perform some simple HTTP benchmarks on both applications, returning HTML for login pages.
Concurrency number
Average delay
Request/second
Transfer/second
Unfortunately, once I increase the number of concurrent "users" to over 100, the Laravel application (or PHP-FPM socket) keeps crashing.
NetData provides the following chart to see how the server can withstand such loads.
Go language, the number of concurrent connections is 100
Laravel, the number of concurrent connections is 100
Note that I run benchmarks from the same machine as the application runs, so this seriously affects both charts.
Let's compare the number of lines of code in both applications, including all vendor dependencies.
<code>find . -name '*.php' | xargs wc -l 156289 total </code>
Laravel version contains more than 156,000 lines of code. This does not include development dependencies, which are necessary for Laravel to run tests, etc.
<code>find . -name '*.go' | xargs wc -l 33624 total </code>
On the other hand, the Golang version contains 33,000 lines of code. This is just one fifth of the same function code.
Let's exclude external dependencies in the Laravel application so that we know how many lines of code I actually wrote.
<code>find . -name '*.php' -not -path "./vendor/*" | xargs wc -l 13921 total </code>
For Golang.
<code>find . -name '*.go' -not -path "./vendor/*" | xargs wc -l 6750 total </code>
If you only view the number of managed lines of code, the results will be more balanced. Still, it's an application with the same functionality, with a halving amount of code.
In Golang, the test is a first-class citizen and the test file is located next to the actual source file.
<code>license.go license_test.go subscription.go subscription_test.go</code>
This makes application test-driven development very convenient.
In our Laravel application, we mainly conduct integration tests to check if the request handler returns the correct response. Overall test coverage is quite low, mainly because of tight coupling, which in turn is mainly my own fault. Writing the same app the second time does help here.
I did something you should never do: I rewritten an app in a different language because I felt like that. The result is a smaller, faster app and a lot of fun.
PHP and Go are both powerful programming languages, but they have some key differences. PHP is a scripting language mainly used for web development, while Go is a statically typed compiled language designed to improve efficiency and simplicity. Go provides excellent performance in speed and memory usage, and is also easier to maintain due to its simplicity and strong typed nature. However, PHP has a larger community and more available resources, which makes learning and using it easier.
There are several reasons to consider migrating from PHP to Go. Go provides excellent performance, which is crucial for applications that require high speed and efficiency. It is also simpler and more direct, which can make your code easier to maintain. Additionally, Go has strong support for concurrent programming, which may be a major advantage for some types of applications.
The difficulty of migrating from PHP to Go varies depending on your familiarity with programming concepts and your willingness to learn a new language. Go's design is simple and easy to understand, but it does have some unique features that take some time to adapt. However, with some effort and practice, most developers should be able to complete the conversion.
There are many resources to help you learn Go. The official Go website provides comprehensive tutorials and documentation, and there are many online courses and tutorials available. Additionally, the Go community is active and supportive, and there are many forums and discussion groups where you can ask questions and get help.
Yes, you can use Go for web development. Go has a built-in net/http package that makes it easy to build web applications, and there are several web frameworks available for Go. However, remember that Go is a common language, so it is not specifically used for web development like PHP.
Go has strong support for concurrent programming, which is one of its main advantages over PHP. Go's goroutine and channels make it easy to write concurrent code, and the language's design ensures the efficiency and security of this code. By contrast, PHP does not have built-in concurrency support, although it can be implemented using other tools and libraries.
Go generally provides better performance than PHP. It is a compiled language, which means it runs directly on the machine's hardware, thus reducing execution time. Go also has a more efficient memory management system, which can reduce memory usage. However, remember that for small, simple applications, the performance differences may not be obvious.
Yes, Go is suitable for large and complex projects. Its simplicity and strong typed features make it easier to maintain large code bases, and its performance features make it suitable for high-load applications. Furthermore, Go's support for concurrent programming may be a major advantage for projects that require high levels of parallelism.
PHP has a larger community and more available resources, which makes learning and using it easier. However, the Go community is growing rapidly and there are already many libraries and tools available for Go. Additionally, Go's simplicity and performance characteristics make it popular among many tech companies, which also contributes to its growing ecosystem.
There may be some challenges when migrating from PHP to Go, including learning new syntax and programming paradigms, adapting to Go's unique features such as goroutines and channels, and finding the right libraries and tools for your needs. But these challenges can be overcome with some effort and the right resources.
The above is the detailed content of Jumping from PHP to Go: Blasphemy, Bravado or Common Sense?. For more information, please follow other related articles on the PHP Chinese website!