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 ...
For further actions, you may consider blocking this person and/or reporting abuse
This is a "no go" for me.
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.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.
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.
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?
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.
Can second this
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.
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 :)
This would be much better served by satisfying the Stringer interface:
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.
Thanks for pointing that out. I just wrote the method as an example, nothing more.
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?
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!
It's definitely worth checking out. I like it because it stretches my brain in ways that PHP and JavaScript do not.
golang.org/x/tour/gotour has moved to golang.org/x/tour
GO seems combining features of several languages. Package system from Java, Structs from C, Pointers from C. Indentation from python. Seems powerful.