DEV Community

Sasha Blagojevic for JSGuru

Posted on • Originally published at jsguru.io

Go for PHP or Any Other Developer

Long time no read! I’ve been quite busy building awesome stuff for our clients so I haven’t had the time to write in quite a while. I still don’t have it to be frank, but this is gonna be a somewhat short read so I managed to squeeze it into my schedule.

TLDR

If you’re not interested in the story behind this list just skip to the end. I highly recommend covering these resources if you’re considering learning GO.[c]

Why Go suddenly?

I’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.

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 Functional and Object Oriented Paradigm. 

To add fuel to the fire GO lang has its own twist with its concurrency model - goroutines and channels.

When your client demands GO lang as the power train you can be pretty sure it’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!

I’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.

Major Differences between PHP and GO

Aside from the obvious fact that GO is a compiled language these are some of the major differences:

  • Switch statement does not fall through by default, you have to explicitly add the fallthrough keyword
  • For is the same as While, that’s the only loop keyword
  • There are no Classes / Objects, there are Structs which have some of the behaviour, they can have members and methods defined on them
  • There are no explicit access modifiers like public, protected and private but there is a naming convention. If you declare a variable / type  / 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.
  • Since there are no Classes there is no Inheritance, but we can achieve Polymorphism because GO has Interfaces.
  • 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 TypeScript’s any. 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 Struct that does not satisfy the type hinted Interface your application will fail.
  • There are no exceptions in a traditional sense, there are no try / catch blocks although there is a proposal to introduce try. What we have though are panics and recover, 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.

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.

That’s the whole point behind language style guides, admittedly some have a more strict some less, some don’t have one at all - PHP didn’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.

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:

Must Read / Watch List

1. Official Go Code Review Comments

2. Go Standard Project Layout (Community)

3. Ben Johnson - Standard Package Layout (Community)

4. Go Docs - Package Naming

5. Peter Bourgon - Go Best Practices Six Years In (QCon London 2016)

6. Peter Bourgon - Go For Industrial Programming (GopherCon EU 2018)

7. Ashley McNamara + Brian Ketelsen. Go best practices (GopherCon Russia 2018)

8. Andrew Gerrard GoLand Talks 2014 (Google)

9. David Crawshaw - Organizing Go Code 2014 (Google)

Top comments (0)