Back to posts
01 Sep 2024

How writing Go improved my PHP code

I have been learning Go for about 2 years. I have been writting lots of small projects and apps on the weekend and I have noticed that since I started learning Go I have had a dramatic improvement in my PHP code.

Handling errors

If you have ever written Go you will know that error handling is a first class citizen within the language. I have found myself adding WAY MORE error handling in my PHP code which has been a major improvement.

Example previously I would probably never check if json_decode completed without an error, but now you will always find an if check after a decode in my PHP code now.

Avoiding else

This is less a go item and more just a simplicity optimization I have made in almost all my code. I avoid the else {} statement at all costs. In most cases you can replace else with an early return and it can greatly impove readability of your code.

Depending on interfaces and returning implementation

If you have ever written Go for more than about a day you will come across an interface. Interfaces where something I almost never used in PHP until recent years. I never really saw the point in having interface for anything other than a factory.

Since I have started writting Go I have found interfaces to be a fantastic tool for improving my testing strategy in my code by ensuring I only depend on the interfaces and not the direct implementations of logic.

This also helps with testing my intended behaviour and not the implementation I am providing, this greatly improves my tests and also ensure I can move and refactor quickly when needed.

Using less dependencies

While there are many packages for Go the unspoken guidance in Go is to not import dependencies unless they are really needed and instead just copy them into your project or write you own implementations.

While at first this seems like just re-inventing the wheel most of the time I find great peace of mind in knowing I own the dependency and can change it to handle my use-case at any point.


Go is a great language with many great prinicples that I really enjoy. It is not perfect and I still reach for PHP for most day-to-day work but I have really enjoyed learning things that I can then apply to my own PHP code to be better and of a higher quality.