Go for PHP or Any Other Developer
Tags: go, php
Published: 2020-01-27 10:23:01

---
title: Go for PHP or Any Other Developer
published: true
description: I’m of the opinion that each language should be written in its idiomatic way, that is the way to get the most out of the language and in addition your code will be easily understandable by the wider community and new developers that might join you or replace you later on. I highly recommend covering these resources if you’re considering learning GO.
tags: go, php
canonical_url: https://jsguru.io/blog/go-for-php-or-any-other-developer
---

<p class="c5"><span class="c0">Long time no read! I&rsquo;ve been quite busy building awesome stuff for our clients so I haven&rsquo;t had the time to write in quite a while. I still don&rsquo;t have it to be frank, but this is gonna be a somewhat short read so I managed to squeeze it into my schedule.</span></p><h2 class="c8" id="h.i61btza3x0es"><span class="c3">TLDR</span></h2><p class="c5"><span>If you&rsquo;re not interested in the story behind this </span><span class="c15">list</span><span>&nbsp;just skip to the end. I highly recommend covering these resources if you&rsquo;re considering learning</span><span>&nbsp;GO.</span><sup><a href="#cmnt3" id="cmnt_ref3">[c]</a></sup></p><h2 class="c8" id="h.nzbnrmddxobs"><span class="c3">Why Go suddenly?</span></h2><p class="c5"><span class="c0">I&rsquo;ve been helping out on an awesome big data / IoT project which is powered by none other then GO! Our client was coming from a Python background and when you add the terms Big Data and Internet of Things to this mix you can clearly see why GO is a natural fit for this project. </span></p><p class="c5"><span>It has been really interesting switching from PHP to Go lang. I must admit at the beginning it was a bit of a challenge to wrap my head around all of the different concepts. Not only was I switching from a scripting language to a compiled one, but Go lang is also a functional language, where as PHP supports both the </span><span class="c4 c1"><a class="c2" href="https://www.google.com/url?q=https://sasablagojevic.com/object-oriented-programming&amp;sa=D&amp;ust=1580123772171000">Functional</a></span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://sasablagojevic.com/object-oriented-programming&amp;sa=D&amp;ust=1580123772171000">&nbsp;and </a></span><span class="c4 c1"><a class="c2" href="https://www.google.com/url?q=https://sasablagojevic.com/object-oriented-programming&amp;sa=D&amp;ust=1580123772172000">Object Oriented Paradigm</a></span><span>.</span><span class="c0">&nbsp;</span></p><p class="c5"><span>To add fuel to the fire GO lang has its own twist with its concurrency model - </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://gobyexample.com/goroutines&amp;sa=D&amp;ust=1580123772172000">goroutines</a></span><span>&nbsp;and </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://gobyexample.com/channels&amp;sa=D&amp;ust=1580123772172000">channels</a></span><span class="c0">.</span><p class="c5"><span class="c0">When your client demands GO lang as the power train you can be pretty sure it&rsquo;s not going to be a walk in the park, you can expect it to be a high performance / high load type of a project so there will be no cutting corners and optimization needs to be on your mind all the time!</span></p><p class="c5"><span class="c0">I&rsquo;d consider my role in this project as kind of a sheepdog since I was setting the path and guiding our junior developers as they do tend to behave like lost sheep sometimes. Client being the shepherd in this analogy I guess lol.</span></p><h2 class="c8" id="h.mzc1zd1v7tsl"><span class="c3">Major Differences between PHP and GO</span></h2><p class="c5"><span class="c0">Aside from the obvious fact that GO is a compiled language these are some of the major differences: </span></p><ul class="c6 lst-kix_msq2adfln5ht-0 start"><li class="c5 c11"><span class="c1">Switch</span><span class="c0">&nbsp;statement does not fall through by default, you have to explicitly add the fallthrough keyword</span></li><li class="c5 c11"><span class="c1">For</span><span>&nbsp;is the same as </span><span class="c1">While</span><span class="c0">, that&rsquo;s the only loop keyword</span></li></ul><ul class="c6 lst-kix_8p6t0vm9se2x-0 start"><li class="c5 c11"><span>There are no Classes / Objects, there are </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://gobyexample.com/structs&amp;sa=D&amp;ust=1580123772174000">Structs</a></span><span class="c0">&nbsp;which have some of the behaviour, they can have members and methods defined on them</span></li><li class="c5 c11"><span>There are no explicit access modifiers like </span><span class="c4 c1"><a class="c2" href="https://www.google.com/url?q=https://www.php.net/manual/en/language.oop5.visibility.php&amp;sa=D&amp;ust=1580123772174000">public, protected and private</a></span><span>&nbsp;but there is a naming convention</span><span class="c0">. If you declare a variable / type &nbsp;/ function lowercase first it will be treated as private on the package level, if you define a member / method lowercase first on a Struct it will not be accessible publicly. On the other hand if you write them uppercase first it will be publicly accessible.</span></li><li class="c5 c11"><span>Since there are no Classes there is no Inheritance, but we can achieve Polymorphism because GO has </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go&amp;sa=D&amp;ust=1580123772175000">Interfaces</a></span><span class="c0">.</span></li><li class="c5 c11"><span>Interfaces in GO have their own twist and are a bit different than in PHP. While in PHP you have to specify which class implements what interface in GO every data type implements an empty interface{} by default. You can think of this interface{} as something similar to </span><span class="c4 c1"><a class="c2" href="https://www.google.com/url?q=https://www.typescriptlang.org/docs/handbook/basic-types.html%23any&amp;sa=D&amp;ust=1580123772175000">TypeScript&rsquo;s any</a></span><span class="c1">.</span><span>&nbsp;When you create your own custom interfaces you will need to add the implementation of these methods on the desired structs. GO will not check if the interfaces are satisfied during the build process, unlike some other compiled languages, it will be evaluated during runtime when the actual method is called, if you call the method on a </span><span class="c1">Struct</span><span>&nbsp;that does not satisfy the type hinted </span><span class="c1">Interface</span><span class="c0">&nbsp;your application will fail.</span></li><li class="c5 c11"><span>There are no exceptions in a traditional sense, there are no try / catch blocks although there is a </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://github.com/golang/go/issues/32437&amp;sa=D&amp;ust=1580123772176000">proposal to introduce try</a></span><span>. What we have though are </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://blog.golang.org/defer-panic-and-recover&amp;sa=D&amp;ust=1580123772176000">panics and recover</a></span><span class="c0">, but recover can only be used to recover from a panic that happened on different goroutine. How do we handle errors then? Well, GO functions have multiple return values. Usually the first value is the desired result and the second value is the error if there is any, otherwise nil. </span></li></ul><p class="c5"><span class="c0">I&rsquo;m of the opinion that each language should be written in its idiomatic way, that is the way to get the most out of the language and in addition your code will be easily understandable by the wider community and new developers that might join you or replace you later on. </span></p><p class="c5"><span class="c0">That&rsquo;s the whole point behind language style guides, admittedly some have a more strict some less, some don&rsquo;t have one at all - PHP didn&rsquo;t have one for a long time before PSRs came into play and our community was honestly a mess, a million of different styles and approaches that just unnecessarily added to our cognitive load. </span></p><p class="c5"><span class="c0">That being said this is a list of resources that you most definitely should read if you want to write idiomatic Go and follow community best practices:</span></p><h2 class="c8" id="h.n614kz22bv9f"><span class="c3">Must Read / Watch List</span></h2><p class="c5"><span class="c1">1. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://github.com/golang/go/wiki/CodeReviewComments%23package-names&amp;sa=D&amp;ust=1580123772178000">Official Go Code Review Comments</a></span></p><p class="c5"><span class="c1">2. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://github.com/golang-standards/project-layout&amp;sa=D&amp;ust=1580123772178000">Go Standard Project Layout (Community)</a></span></p><p class="c5"><span class="c1">3</span><span>. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://medium.com/@benbjohnson/standard-package-layout-7cdbc8391fc1&amp;sa=D&amp;ust=1580123772178000">Ben Johnson - Standard Package Layout (Community)</a></span></p><p class="c5"><span class="c1">4</span><span class="c1">. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://blog.golang.org/package-names&amp;sa=D&amp;ust=1580123772179000">Go Docs - Package Naming</a></span></p><p class="c5"><span class="c1">5</span><span class="c1">. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://peter.bourgon.org/go-best-practices-2016/&amp;sa=D&amp;ust=1580123772179000">Peter Bourgon - Go Best Practices Six Years In (QCon London 2016)</a></span></p><p class="c5"><span class="c1">6</span><span class="c1">. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://peter.bourgon.org/go-for-industrial-programming/&amp;sa=D&amp;ust=1580123772180000">Peter Bourgon - Go For Industrial Programming (GopherCon EU 2018)</a></span></p><p class="c5"><span class="c1">7</span><span class="c1">. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://www.youtube.com/watch?v%3DMzTcsI6tn-0&amp;sa=D&amp;ust=1580123772180000">Ashley McNamara + Brian Ketelsen. Go best practices (GopherCon Russia 2018)</a></span></p><p class="c5"><span class="c1">8</span><span class="c1">. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://talks.golang.org/2014/names.slide%231&amp;sa=D&amp;ust=1580123772180000">Andrew Gerrard GoLand Talks 2014 (Google)</a></span></p><p class="c5"><span class="c1">9</span><span class="c1">. </span><span class="c4"><a class="c2" href="https://www.google.com/url?q=https://talks.golang.org/2014/organizeio.slide%231&amp;sa=D&amp;ust=1580123772180000">David Crawshaw - Organizing Go Code 2014 (Google)</a>