DEV Community

Cover image for Introduction to Go for PHP Developers

Introduction to Go for PHP Developers

Andrew Davis on July 06, 2018

Recently, I started working on an internal CLI app for my team at work. My main programming language of choice is PHP, but I wanted to create this ...
Collapse
 
dopitz profile image
Daniel O. • Edited

In Go, errors are not treated like exceptions. There is no throw or catch mechanism. Instead, errors are returned from functions if one has occurred.

This is a "no go" for me.

Collapse
 
ameliagapin profile image
Amelia Gapin

At first, I wasn't a fan of this. It made the code feel cumbersome. After a bit, I not only got used to it, but came to embrace it. It makes error handling much more a natural part of the way you code. You can avoid the scoping of a try/catch block because you don't have to put your code into another block.

The compiler will ensure that you've recognized the error is part of the return (when using a multi-return function) so you don't have to worry about being unaware of it. If you do anything with it and what you do is up to you. You could choose to ignore it by assigning it to _ instead of a variable.

Collapse
 
restoreddev profile image
Andrew Davis

It’s not as bad as you might think. It forces you to account for each error which makes your code a little more resilient.

Collapse
 
david_j_eddy profile image
David J Eddy

I read that same line and instantly thought `oh, crap. error catching is going to be a mess'. But then I thought about it; most error catching I do should be handled in a much more graceful way. Not a 'here is an error, return stack trace'. After reading this article I feel like going Go a try, again.

Collapse
 
okolbay profile image
andrew • Edited

4 steps to love it:
1 try to implement clean project structure
2 use exceptions to propagate errors
3 realize that exception is part of interface, and to avoid coupling, or catching SQL exceptions in HTTP controller you need exception per layer
4 give up and embrace go’s way of handling errors

not to mention that throw is essentialy a GOTO instruction, its slow and ask yourself - how many times, when catching β€œbusiness-logic” related exception from your code you were interested in stacktrace?

Collapse
 
igormp profile image
Igor Moura

I honestly love it. As a hardcore C user, it feels pretty natural to me and makes you deal with the error as soon as possible instead of ignoring it.

Collapse
 
presto412 profile image
Priyansh Jain

Can second this

Collapse
 
brpaz profile image
Bruno Paz

Great article.

I have been working in both PHP and Go on the last months in my company, and I think they really complement each other very well. Some of the known weak points of PHP like concurrency or long running processes are the strengths of Go.

Plus, Go is really great for applications like system tools, command line tools or small services (ex: slack bots, webhooks listeners, etc), because since it compiles into a single binary makes it incredibility easy to install and run in any system.

PHP is still one of my favorite languages for general web application development as I can write clean code really fast, thanks to OOP design and frameworks like Symfony.

Adding a mix of Go for more specific use cases and you can build almost anything.

So,if you are a PHP developer, I really recommend taking the time to learn Go.

Collapse
 
fzammit profile image
Fabio Zammit

Great insights.

I am curious to see whether you had an instance where GO would consume the data received by the Symfony APP i.e. listening to some form of events etc? If so could you please give some examples as to how you went about this.

Thanks :)

Collapse
 
ladydascalie profile image
Benjamin Cable • Edited
func (c Cup) nameAndColor() string {
  return c.name + ": " + c.color
}

This would be much better served by satisfying the Stringer interface:

func (c Cup) String() string {
     return c.name + ": " + c.color
}

I would recommend following the tour here: tour.golang.org/methods/17
To see in greater detail why this is a more useful way to do this.

Collapse
 
restoreddev profile image
Andrew Davis

Thanks for pointing that out. I just wrote the method as an example, nothing more.

Collapse
 
jay97 profile image
Jamal Al

hey, you said Go is a"program in a language that could run on any platform without having to have an interpreter already installed". Could you explain how that works, so you can write Go App and run it on other machines without having to install some kind of interpreter?

Collapse
 
blackcat_dev profile image
Sasha Blagojevic

I was just thinking about picking up Go, when your post popped up on Facebook, as if they were reading my mind...or search history...God damn you Zuck, you lizard!

Collapse
 
restoreddev profile image
Andrew Davis

It's definitely worth checking out. I like it because it stretches my brain in ways that PHP and JavaScript do not.

Collapse
 
maxdevjs profile image
maxdevjs

golang.org/x/tour/gotour has moved to golang.org/x/tour

Collapse
 
inambe profile image
Inam Ul Haq

GO seems combining features of several languages. Package system from Java, Structs from C, Pointers from C. Indentation from python. Seems powerful.