Best way to use Go alongside PHP?

agolangf · · 954 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m a web developer that mostly writes in PHP and I&#39;ve been working on a project that is heavily written in PHP7 which is fine it&#39;s fairly fast (except for a few things), but ever since I started writing golang <em>(mainly because I started to get burnt writing the same language during the day and evenings, go was a bit refreshing break from my daily work)</em> I started to think that I could rewrite some things using go that I&#39;ve written that was taking a toll on the project.</p> <p>Problem is I&#39;ve never mixed two languages together to accomplish a job. Now I&#39;ve always read about people using other languages to make slow tasks faster, but I&#39;ve never actually read anything on the subject of actually doing it.</p> <p>So what&#39;s the best approach to actually doing that and what are the best practices when running golang alongside another language?</p> <hr/>**评论:**<br/><br/>daveddev: <pre><p>Create the most easily modularizable services first. Wire up the PHP to use those services. Break off more and more functionality into services and continue winding down the need for the old code.</p></pre>_Skuzzzy: <pre><p>Even if the services are running as the same computer as the PHP that is gonna hit him with some pretty big overhead, even more if he has to marshal data. </p> <p>If he runs the services on another machine god help him.</p></pre>daveddev: <pre><p>If enough data was being processed for these things to be of real concern, using Go services would more than likely alleviate strain caused within the existing PHP solution.</p> <p>If you still feel that you have a case, please clarify the bottlenecks and how they would be better served by using PHP (assuming that a general micro-services approach is being taken).</p></pre>ecmdome: <pre><p>This all depends on the use cases... we run them on different servers(Java, php and Scala)... there are different caching layers to reduce some overhead... be more specific with this &#34;overhead&#34; you speak of.</p> <p>Edit: not coaching... caching -_-</p></pre>SeerUD: <pre><p>It sounds like you&#39;ve not heard of micro-services, or service-oriented architecture. This kind of thing is actually very common, and there are ways to minimise that overhead, for example, using things like <a href="https://developers.google.com/protocol-buffers/" rel="nofollow">protobuf</a>.</p></pre>ecmdome: <pre><p>At work we have 3 seperate codebases that work along side eachother... slowly converting to one code base.</p> <p>Basically we have our load balancers sending traffic for certain endpoints to one server while others go to another.</p> <p>So you can start separating endpoints from php to go and gave haproxy send the endpoints to the correct server.</p> <p>To share data between the two you could always make endpoints that produce JSON or XML data that golang can read from the php endpoint or vice versa.</p> <p>Hope that makes sense.</p></pre>tty5: <pre><p>I&#39;m currently rewriting a moderately large legacy PHP app in Go.</p> <ol> <li>Get sessions from legacy app to work in go</li> <li>Rewrite one endpoint in Go</li> <li>Update Nginx config so that endpoint is handled by go, not php</li> <li>Goto 2, repeat until no php is left</li> </ol> <p>If the original app is a monolith don&#39;t get yourself into divide-everything-into-microservices rabbit hole. </p></pre>zarko_popovski: <pre><p>Whenever I need to write a backend and API for the native mobile project I am writing web admin with PHP, Codeigniter with Bootstrap and depending on expected number of concurrent users I am writing the API in Go.</p></pre>no1youknowz: <pre><p>I do the exact same things for my web application.</p> <p>PHP + Jquery + Bootstrap allow me to do things in hours. Mock up ideas in minutes. Very quick and very fast. A lot of times, I&#39;m already leveraging code I already have written.</p> <p>It&#39;s great for web admin front-ends.</p> <p>However, when I have a task. I use go for that. Image uploading, image resizing, heavy duty manipulations of data, multiple database operations of batch data, web scraping, email sending, the list goes on.</p> <p>Fact is. Go runs rings around PHP for tasks where backend processing is a huge factor.</p> <p>So, treat each language as separate. PHP for the front. Go for the back.</p> <p>Of course, Go can replace PHP on the front-end. However, for large monolithic type applications. You can&#39;t quite organise Gos folder structor the same as you can with PHP. </p> <p>I love Go and I was happy I took the time to learn it.</p></pre>SerialMiller: <pre><blockquote> <p>However, for large monolithic type applications. You can&#39;t quite organise Gos folder structor the same as you can with PHP. </p> </blockquote> <p>What do you mean by this?</p></pre>eugene-d: <pre><p>Read Sam Newman&#39;s book about microservices. </p></pre>watermanio: <pre><p>Start with the bits php doesn&#39;t do so well and replace those with go - either accessed entirely over HTTP with nginx sending traffic to specific paths to your go app instead of the php one, or do what my work have done, and send all traffic to go, then go sends back to php the requests it doesn&#39;t know how to handle (most of them, at this time). </p></pre>twoism: <pre><p><a href="http://grpc.io" rel="nofollow">gRPC</a> </p></pre>yatseni: <pre><p>Oh, you can also write PHP extension with Go:</p> <p><a href="https://github.com/kitech/php-go" rel="nofollow">https://github.com/kitech/php-go</a></p></pre>interactiv_: <pre><p>Depends on what you intent to write in PHP and in Go. You can write the front-end of a server application in PHP ( the part that deals with HTML generation ) and some back-end API in Go. You can use Go in an worker queue system where your workers are coded in Go , ... you can call Go executables from PHP to perform a specific task (although in that case it makes more sense to write a server ), you&#39;ll understand pretty quickly what Go is good at and what it is not good at.</p></pre>SaltTM: <pre><p>Mainly backend, rebuilding large data caches, things like that.</p> <blockquote> <p>makes more sense to write a server</p> </blockquote> <p>Can you elaborate here?</p></pre>interactiv_: <pre><p>You don&#39;t want to spawn a new process each time you need to execute a binary, it&#39;s better to write a server that will queue calls so you can be in control of how many processes are executed in parallel (on the go side).</p></pre>cfsalguero: <pre><p>Use PHP to run Drupal and write a blog about Go</p></pre>itsmontoya: <pre><p>Take the PHP code and burn it.</p></pre>SaltTM: <pre><p>I love the direction PHP is going and 7 is amazing, I also really enjoy golang. What I&#39;m trying to say is, don&#39;t be a dick.</p></pre>schumacherfm: <pre><p>They achieved some nice things with PHP7 (compared to 5) but the whole RFC system adds to much bloated features to PHP. One example which got eventually declined: <a href="https://wiki.php.net/rfc/union_types" rel="nofollow">https://wiki.php.net/rfc/union_types</a> Therefore I love Go that the language is done.</p></pre>SaltTM: <pre><p>eh.. subjective, a lot of things that made 7.1 I wouldn&#39;t consider bloat <a href="https://wiki.php.net/rfc#php_71" rel="nofollow">https://wiki.php.net/rfc#php_71</a>, but yeah going to drop this discussion as it&#39;s getting a wee bit off topic.</p></pre>grutoc: <pre><p>Your comment is basically useless, <em>go get</em> social recognition elsewhere.</p></pre>NeoArc: <pre><p>I think you replied to the wrong comment.</p></pre>grutoc: <pre><p>I don&#39;t think so.</p></pre>NeoArc: <pre><p>Maybe I should be helping You instead with some question on reddit. Would that be better?</p></pre>Carpetsmoker: <pre><p>Yes yes, there is much to be said about the crappyness of PHP, and I would probably agree with much of it. But this is neither the place nor the time. Fact is PHP exists and is used a lot. Deal with it.</p></pre>RevMen: <pre><p>It could attack at any time.</p></pre>eugene-d: <pre><p>Used PHP for 10 years and now write Go. PHP 5+ is okay. I would still use it for server rendered websites. You clearly have no experience with good OOP PHP </p></pre>itsmontoya: <pre><p>That&#39;s a very naive statement to make, friend.</p></pre>interactiv_: <pre><blockquote> <p>Take the PHP code and burn it.</p> </blockquote> <p>typical gopher spotted .</p></pre>0xembark: <pre><p>I&#39;m not what you&#39;d call a gopher, but I still hate PHP.</p> <p>Hating shitty languages transcends what kind of coder you are.</p></pre>interactiv_: <pre><p>Typical gopher = typical hater in the go community. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

954 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传