Professional Documents
Culture Documents
Moving From PHP To Go and Learning To Love Them Both
Moving From PHP To Go and Learning To Love Them Both
José Carlos Chávez
Software Engineer @Typeform. Wine lover and Llama ambassador
Feb 27 · 9 min read
Moving from PHP to Go and learning to
love them both
Irrelevant picture for the post just to discourage you to buy ivory
A few days ago I read this post and remembered mixed feeling and
some “heated” discussions that I had with my colleagues about Go. At
that time I was just starting out with Go after working primarily with
PHP for the previous 5 years.
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 1/9
3.6.2018 Moving from PHP to Go and learning to love them both
The beginnings
It is impossible not to draw comparisons when moving from one
language to another so my very rst impressions about Go were not as
tolerant as they are now:
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 2/9
3.6.2018 Moving from PHP to Go and learning to love them both
<?php
try {
functionA();
functionB();
} catch (ExceptionTypeC $e) {
...
} catch (ExceptionTypeD $e) {
...
}
If you really want it to understand where the errors have come from,
you might need to do something like
<?php
try {
functionA();
} catch (ExceptionTypeC $e) {
...
}
try {
functionB();
} catch (ExceptionTypeD $e) {
...
}
Which is essentially
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 3/9
3.6.2018 Moving from PHP to Go and learning to love them both
err := functionA()
if err != nil {
...
}
err = functionB()
if err != nil {
...
}
On the other hand, what I liked about Go from the very beginning was:
• Go does not have inheritance (thank God for that): Instead you
have to use some other techniques such as embedded objects
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 4/9
3.6.2018 Moving from PHP to Go and learning to love them both
not in compile time, and, although there are tools such as phpstan
that help you overcome this limitation, it is really nice that in Go it
is a built in feature of the language.
• I don’t agree with the one letter variable , I think over the past
40 years the software industry has agreed that developers spend
much more time reading code than actually writing it, meaning
that the code should be written thinking about readers. I don't
want to have to remember that r most likely stands for Request
My opinions about the Ivan Jaros’ post
I would really like to share my opinions about the post that spurred me
on to create this blog post, bear in mind my opinions are based on my
experience working with Go and PHP.
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 5/9
3.6.2018 Moving from PHP to Go and learning to love them both
I can understand the pain. I am a Maths student, and for my studies I’ve
been working with C for the past 10 months. I must confess that every
time I nished something I told myself: “I could write this in half of the
time with Go”. Even more I pair programmed with a colleague in a kata
some months ago and we really missed Go’s simplicity and syntax over
C.
Go’s simplicity works very well in your favour if you are creating a
single purpose low-level library or application that is small in size and
purpose.
But another point I want to make is that in Go, you absolutely, under no
circumstances, should be even trying to reuse code or de ne an
interface. Sooner or later, it will bite you in the ass. […] For example,
Go would be perfect language to use for a micro service whose purpose
is to receive events from event store in event sourced architecture and
normalize them into consumable messages for the message broker to
distribute to interested 3rd parties. Now imagine a microservice where
you have to connect to a database, fetch some results, perform some
business logic and send some messages out.
At Typeform we use Go not only for low level tasks/tools but also for
writing microservices using DDD and CQRS. Go has proven compatible
with this and it also made me realize that sometimes we abuse of
classes in order to make the so called Domain objects. In addition, one
feature that I love in Go (and from C actually) is how easy is create
custom types and associate them with some behaviour.
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 6/9
3.6.2018 Moving from PHP to Go and learning to love them both
When it comes to Go, spaghetti code is what you want to write. Go has
composition instead of inheritance and polymorphism but you will be
running in circles if you’ll try to create any re-usable code. You will fail
badly.
Conclusions
After some months working with Go, some of my dislikes for the
language became my likes, so right now if I am asked about what do I
prefer I would reply: It depends.
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 7/9
3.6.2018 Moving from PHP to Go and learning to love them both
https://medium.com/devthoughts/moving-from-php-to-go-and-learning-to-love-them-both-45847de0266c 8/9