Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's not my experience. Other then the recover included in the http lib, I don't think I have ever used recover.

Why is you code panic'in? I would let it take down the process and figure out why. I have had backend programs set up to automatically restart, which can be useful. But I would treat any panic as a big deal.



You can treat panics as a big deal, and not necessarily kill the whole program. It's not mutually exclusive.

For example, at my work, we have some nightly long running tasks. We don't panic every day. But from time to time, let's say once or twice per month, some code changes cause a panic. On that day, we don't want to kill the long running tasks for no good reason other than somehow indirectly making someone fix the panic. We have alerts for that, and we're all grownups.


> You can treat panics as a big deal, and not necessarily kill the whole program. It's not mutually exclusive.

Yes it is mutually exclusive. Something that doesn't kill the program, aka a recoverable ERROR CONDITION should not cause a panic, that's not what panics exist for.

Something that causes a panic without using the `panic` keyword, like an out-of-bounds read, nil-derference, etc. is indicative of a serious problem that should be fixed before the program is allowed to run again.


> Something that doesn't kill the program, aka a recoverable ERROR CONDITION should not cause a panic

Can you explain why?

> a serious problem that should be fixed before the program is allowed to run again

Can you explain why the program should not be allowed to run again? Is this some sort of software spiritualism?


> Can you explain why?

Because that is semantically what a panic means in Go. See the link to effective go I posted you elsewhere in this thread.

I am well aware that it can be used in other ways. Same as I can say "car" when talking about a mainline battle-tank. Sure, a car has an engine, runs on fuel and drives on land. There are similarities. The words still mean very different things.

And I am also sure there have been instances of someone using a tank to go order food at a drive-through. Doesn't mean that it is semantically correct to do so, or advisable.


Your position is basically "there should be no recover() because you should never recover". Go itself disagrees, no matter how you interpret some "effective go" directive. See how many times it's used in the stdlib:

https://github.com/search?q=repo%3Agolang%2Fgo%20recover()&t...

It just doesn't make sense to take down the whole server, including all requests / jobs in flight, because there's some nil deref or out-of-bounds. Yea, that thing has to be fixed, but sending a specific alerts is much better than indirectly alerting by taking the whole system down.

If you're using go for something non-web, then it may very well make sense to not have recovery anywhere. Except of course you do have some, in the stdlib. But you can apply it to your code, if you want.

But it can't be some universal pragma (or convention) in go, as it violates the stdlib.


As I have said before, the fact that it is used, doesn't validate the usage. Yes, Go has recover. Yes, the stdlib uses it here and there.

Does any of that change the semantics of what a panic means, and how applications should therefore react? No. Does it make panics the equivalent of exceptions in Python semantically? Also no.

And this logic isn't limited to Go. Guess what, there are python libraries that use Exceptions for control flow. It certainly works. Does that validate using Exceptions as control flow elements? No, of course not. Why? Because that's not what an exception exists for semantically.

Panic-Recover cycles in go codebases are an antipattern, and unless I see an official statement by the people who make Go (who also write "Effective Go" btw.) saying otherwise, those are the semantics of the language.


It indicates that either programmer or at least operator (if E.G. this is a critical data validation fail), some sort of human, intervention is required. The situation must be evaluated critically before any further work occurs.


>your code

3P code is a thing

>why

Sometimes there are edge cases with nil pointers that testing missed.

>automatically restart

What about all of the other requests in flight? Letting those fail because one request hit an edge case isn't great for a production service with high throughput.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: