又一次从GO迁移到PHP - Go语言中文社区

又一次从GO迁移到PHP


原文链接 https://dannyvankooten.com/from-go-back-to-php-again/

Moving from Go to PHP again

又一次从GO迁移到PHP

Remember when I ditched Laravel for Golang?
还记得我当时为了GO放弃Laravel吗?

Well, after 2 years on Go, our shop applications are powered by PHP again.
好吧,在用了两年GO语言之后,我们的购物应用又一次用PHP来支撑

Why?! You already said it was probably a bad business decision, and then you spend even more time on it?! Well, yeah, several reasons actually.
为什么?你可能已经说这大概是一个糟糕的商业决定,你已经花了大把的时间在它GO上 是的,原因有如下几点

PHP improved a lot

PHP 改进了很多

PHP improved a lot during the last 3 years. It added scalar argument type declarations, return type declarations, multi-catch exceptions, impressive performance improvements and many more general improvements.
在过去的三年里,php改进了非常多,它添加参数类型声明,返回类型声明,多异常捕获,和一些让人印象深刻的性能提升和很多其他改进

Symfony4 is a game changer

Symfony4 是一个改变游戏规则的框架

I’ve always been a big fan of Symfony’s compatibility promise and their impressive 13-year track record proves they mean it.
我一直是Symfony兼容性承诺的忠实粉丝,他们让人印象深刻的13年来的历史记录也证明了这一点

So when Symfony4 was released and I heard good things about it, I took it for a test drive by implementing a tiny part of our application in it.
所以,当Symfony4发布之后是我听到的关于他的一个好消息,我用它实现了一个我们网站的一个小功能当作试验

Conclusion: it’s great. Really, really great.
结论:这非常棒,真的,确实很棒

A lot of effort went into simplifying the setup, making it a lot faster to bootstrap a Symfony application with much less work required configuring bundles. It’s now rivaling Laravel’s rapid development while at the same time encouraging decent development practices to ensure you don’t shoot yourself in the foot. And it performs really well.
为了简化设置付出了很多努力,启动一个Symfony要快的多,用更少的工作量来配置需要的包

It was relatively easy to port our old Laravel application to Symfony, implement some new features the Go version of our application offered and undo some of the shortcuts I took earlier (most of them because of Laravel’s global helpers).

从我们老的Laravel应用迁到Symfony,实现我们应用的GO版本提供了一些新特性并撤销一些我早期的快捷键(他们其中一些是由于Laravel全局帮助函数)相对容易

A nice side effect is that I’ve managed to substantially increase our test coverage in the process. Writing the same application in terms of functionality for a second third time really helps in that regard.
一个很好的副作用是,我在这个过程中大幅增加测试覆盖率,写同样的功能相同的应用~~第二次~~第三次在这方面确实有帮助

Symfony's debug bar

Symfony’s debug bar is an amazing tool. It shows you what happened during the journey from request to response, notifies you of warnings & deprecations and comes with a built-in profiler that you can easily hook into to benchmark parts of your own code.
Symfony的调试Bar是一个非常牛逼的工具,它给你显示了从请求到响应之间发生了什么,提醒你警告和弃用并附带一个内置分析器 ,你可以更容易的链接到你自己的基准代码部分

Symfony's profiler

After learning Symfony’s Form component, I’d rather not go without it again. It makes it trivial to render an accessible form that can be re-used in several places, validating the form upon submit and then populating a PHP object from the form data safely.
在学了Symfony的表单组建之后,我宁可没有它,在一些地方重用可访问表单,验证表单提交,通过表单安全的填充PHP对象,这使他它变得微不足道

$user = $this->getUser();
$form = $this->createForm(UserBillingInfoType::class, $user)
             ->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    // $user is already populated with the form values at this point
    // it's valid, so we can update the database and redirect the user now
}

Doctrine is another piece of software that really improved our overall application. Your models (entities) are normal PHP classes and relations (associations) are normal references, making it easy to test your domain logic without having to worry about the database implementation.

Doctrine确实可以整体改进我们的应用的一款软件,你的Models(实体)也都是PHP普通的类 关系(关联)也是正常的引用,使得测试你域名下的逻辑变得非常简单,不用担心数据实现。

$user = new User();
$user->addLicense(new License());
$manager->persist($user); // both user and its license will be saved

In Doctrine all operations are wrapped in a SQL transaction by default. That’s a big plus for me as it guarantees atomicity, which involved more work to get right in Eloquent.
在Doctrine里,所有操作都被默认包裹在SQL事物中,这对我来说是一个大的好处, 它保证了原子性,这涉及到更多的工作才能在Eloquent中得到正确的东西

Go is (still) great

Go (仍然)很棒

Honestly, Go is great. Its simplicity is refreshing and you can’t get anywhere near that kind of performance using PHP 1. I would still pick it if we need a small API or something that requires high throughput.
老实说,Go很棒,它的简洁让人很清爽,你用PHP无法达到Go但性能,当我们需要一个小的API或者高吞吐量的时候我仍然会选择Go

Our shops however are more monolithic with a lot of server-side rendering. While that’s certainly doable in Go (as the last 2 years proved), it’s more maintainable for us to do it in PHP right now.
然而,我们的应用功能比较单一,有很多服务器渲染。虽然在Go中确实可行(两年的时间证明),我们现在可以用php来维护他

Side note: without the experience gained from our years on Go, I probably wouldn't have started Fathom Analytics. So perhaps it wasn't such a bad business decision after all?
注释:如果没有我们这么些年对Go的使用经验,我不太愿意开始使用Fathom Analytics。大概它不是一个错误的商业决定?

Making the correct business decision

做一个正确的决定

One reason not mentioned so far is that over the last year or so, I’ve been approached by several companies interested to take over one of our products.
还一个原因到现在还没提到,这些年来,我接触的一些公司对接管我们的产品很有兴趣

They were a little surprised to hear our stack involved Golang and some flat out told us they’d prefer PHP, because that’s what most of our products (mc4wp.com, boxzillaplugin.com and htmlforms.io) rely upon. And I don’t blame them.
当他们知道我们的技术栈是Golang的时候有些惊讶,他们更喜欢PHP,因为这是我们大多数产品的依靠,我并不怪他们

1 Just for fun, I compared apples and oranges again by benchmarking the login page (which doesn't hit any database) for both application versions using Siege.
为了好玩,我通过使用Siege对两个版本的应用的登陆页面(没有命中任何数据库)进行基准测试,我又一次对橘子和苹果进行了比较

The Symfony application (PHP 7.3, OPcache enabled, optimized autoloader) handles about 1470 req/s. The Go application (compiled using Go v1.11) averages about 18600 req/s.

版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/3a978af33745
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-09 21:50:53
  • 阅读 ( 1386 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢