News for hackers. Stuff that matters.
Re:A minimal blog engine written in Go, compatible with Ghos... by zura on Saturday April 25, 2015 @03:15PM (#9439287)
Regarding Golang's if-hell e.g. in https://github.com/kabukky/journey/blob/master/database/upda...

Couldn't you use panic/recover locally, inside functions - just for having a single error handling code?

Re:A minimal blog engine written in Go, compatible with Ghos... by pdq on Saturday April 25, 2015 @03:23PM (#9439306)
No, panic/recover is a major hack.

Rob Pike has written ideas here on how to clean up error handling if-blocks: http://blog.golang.org/errors-are-values

Re:A minimal blog engine written in Go, compatible with Ghos... by burntsushi on Saturday April 25, 2015 @05:37PM (#9439656)
No, it's not a "major hack." That blog post is excellent, but it just doesn't always apply.

The panic/recover idiom is extremely useful in parsers. For example, Go's JSON decoder: http://golang.org/src/encoding/json/decode.go

Re:A minimal blog engine written in Go, compatible with Ghos... by pdq on Saturday April 25, 2015 @07:51PM (#9439959)
The difference is that in parsers, errors are almost certainly fatal. As opposed to a runtime SQL error, where it could be a user error, value error, or other database error, which can be recovered from.

Panic/recover are usually meant to catch "coding" and "assertion"-type errors (null pointers, array overflows, etc), not runtime errors. The latter is what the "error" values are used for.

Re:A minimal blog engine written in Go, compatible with Ghos... by burntsushi on Saturday April 25, 2015 @08:32PM (#9440057)
I'm pretty sure you've misunderstood. Did you read the code I linked you to? Look at how it handles errors internally. Go's standard JSON decoder isn't going to crash your program if it fails to parse JSON---that would be disastrous. It exposes regular errors to the user. But internally, it uses panic/recover because it elides a lot of manual error checking.
Re:A minimal blog engine written in Go, compatible with Ghos... by thirsteh on Saturday April 25, 2015 @08:12PM (#9440008)
Panic/recover is no problem as long as it does not traverse API boundaries, i.e. affects the user of your package, unless it really is a "fatal user error" in a function that wouldn't otherwise return an error.
Re:A minimal blog engine written in Go, compatible with Ghos... by superuser2 on Saturday April 25, 2015 @10:08PM (#9440256)
Interesting. That's basically the Maybe monad.
Re:A minimal blog engine written in Go, compatible with Ghos... by lorddoig on Saturday April 25, 2015 @04:03PM (#9439415)
I haven't written go in a while, but I recall being rather dissatisfied with the so-called 'if-hell'. I've been wondering lately - is there any reason why you couldn't/shouldn't use monads for this? Nothing obvious popping out of Google.

"gonads" would be my choice of name for this library.

Re:A minimal blog engine written in Go, compatible with Ghos... by rlp on Saturday April 25, 2015 @04:30PM (#9439480)
Love the name! But there's no way Go's feeble type system supports anything remotely like a Monad.
Re:A minimal blog engine written in Go, compatible with Ghos... by stcredzero on Saturday April 25, 2015 @05:56PM (#9439692)
Here's what GIlad Bracha might say about that:

http://www.infoq.com/presentations/functional-pros-cons?utm_...

Re:A minimal blog engine written in Go, compatible with Ghos... by tptacek on Saturday April 25, 2015 @05:31PM (#9439634)
Since they're wrapping things in transactions anyways, they could just defer the error check to the end (or for that matter rework their interface to the database to stop after the first error is detected).
Re:A minimal blog engine written in Go, compatible with Ghos... by 4ydx on Saturday April 25, 2015 @10:07PM (#9440253)
Re:A minimal blog engine written in Go, compatible with Ghos... by 4ydx on Sunday April 26, 2015 @03:00AM (#9440812)
This is a simple way of dealing with repetitive error handling.
Re:A minimal blog engine written in Go, compatible with Ghos... by arthurbrown on Saturday April 25, 2015 @07:36PM (#9439928)
I've written a tiny package that does just that in a controlled way, using named returns to bubble the error out.

https://github.com/dagoof/failure

[Docs] https://godoc.org/github.com/dagoof/failure

Re:A minimal blog engine written in Go, compatible with Ghos... by dpritchett on Saturday April 25, 2015 @03:25PM (#9439314)
I have never messed with panic, I read that code and I think I'd make a function that takes functions - the error handling response is the same every time. Maybe even put the list of calls in an actual list and invoke them with an iterator.
Re:A minimal blog engine written in Go, compatible with Ghos... by vardump on Saturday April 25, 2015 @07:10PM (#9439860)
Explicit error recovery is not hell, it's a blessing. Much easier to understand and to maintain than exception handling.
Re:A minimal blog engine written in Go, compatible with Ghos... by tomjakubowski on Saturday April 25, 2015 @07:49PM (#9439948)
There is a third way. See Haskell's Either l r, or Rust's Result<T, E> types. See http://lucumr.pocoo.org/2014/10/16/on-error-handling/ and http://lucumr.pocoo.org/2014/11/6/error-handling-in-rust/

I don't know why so often, the immediate reaction to legitimate criticism of this wart in Go is to argue against exceptions, even when nobody has even brought up exceptions as an alternative.

Re:A minimal blog engine written in Go, compatible with Ghos... by thirsteh on Saturday April 25, 2015 @08:20PM (#9440031)
In fact, Either/Maybe can function like safer versions of both paradigms: Functors allow you to build a program of many functions that will terminate with 'Nothing' or 'Either l' as long as any one of them experiences an error, without thinking about errors, and it still lets you explicitly handle any errors if you want to, this time on the sum type Maybe or Either, meaning that you usually handle both the error and non-error case, or receive a compiler warning.

I agree that, probably universally, the people who fervently argue for either exceptions or explicit Go-style error handling don't know about Maybe/Either/Result. Then again, all this is pretty much impossible to replicate in Go.

Re:A minimal blog engine written in Go, compatible with Ghos... by Dewie2 on Sunday April 26, 2015 @12:01PM (#9442152)
Thank you for mentioning this. It's frustrating that every time someone complains about Go's error-handling style, the first assumption is that the complainer then necessarily has to prefer exceptions. In truth, errors-as-values are very elegant in languages like Haskell IMO. And errors-as-values are not at all something that Go has managed to put back on the roadmap in a modern language against the grain of everyone else (the "exception people") as many seem to imply, including Rob Pike in his "errors as values" article.
Re:A minimal blog engine written in Go, compatible with Ghos... by vezzy-fnord on Saturday April 25, 2015 @08:26PM (#9440044)
Option types are nice. The fourth way (and I think the best) is supervision trees, though this is more applicable when you have multiple units of execution... which Go does, so not having something similar out of the box seems strange.

That said, even something as simple as errors being literal values that you can pattern match on is surprisingly versatile. Exceptions don't necessarily have to be awful weights that spill down all over the call stack, either. They can be distilled to a more basic catch/throw mechanism for shuffling around error properties and states.

Re:A minimal blog engine written in Go, compatible with Ghos... by threeseed on Saturday April 25, 2015 @08:46PM (#9440082)
Personally I love option types but working on a large Scala project at the moment it sometimes results in non ideal behaviour from developers especially those coming from the Java world.

What I've seen a lot of is swallowing of errors. For example readFromFile().orElse("") where there is no indication from an outsider that anything wrong happened. A cool feature would be logging any instance of when orElse was the result.

Would be curious how these work in other languages.

Re:A minimal blog engine written in Go, compatible with Ghos... by Jweb_Guru on Sunday April 26, 2015 @12:54PM (#9442353)
The trick is to make it very ergonomic to return the error. I thought Scala had do notation to make this the case. In Rust, the try! macro (which returns on error) is shorter than almost any other way to handle an error, so people usually use that.
Re:A minimal blog engine written in Go, compatible with Ghos... by threeseed on Saturday April 25, 2015 @08:36PM (#9440067)
I'm confused what makes exception handling so troublesome ?

Personally I find them essential in particular for larger codebases. They allow for centralised, consistent error handling behaviour. For example paging application support teams only for certain types of errors.

And you can always emulate explicit error recovery with exceptions. So what is so bad about having the flexibilty to accomodate different styles of error handling.

Re:A minimal blog engine written in Go, compatible with Ghos... by xienze on Saturday April 25, 2015 @10:28PM (#9440298)
Yeah, I don't understand the objection to exceptions either. I _think_ the reason is because people treat them much the same was as errors in C/Go/etc., i.e., "I have to catch them here and recover right away." If you're doing it that way, you're doing it wrong. Design the system such that exceptions can be rethrown (bubbled up) and defer the handling as long as possible.
Re:A minimal blog engine written in Go, compatible with Ghos... by mike_hearn on Sunday April 26, 2015 @02:14PM (#9442691)
Nothing. This is one of those "Go doesn't have it, so it must be bad" things. The way Go handles errors is a throwback to C and was abandoned by literally every single imperative language for good reasons.
Re:A minimal blog engine written in Go, compatible with Ghos... by 4ydx on Sunday April 26, 2015 @03:16AM (#9440850)
Honestly I wish that golang forced you to acknowledge all return values from methods even if you simply ignore them ( _ = Method() ). When I read the docs for any method calls in golang I like being able to see that something returns an error just by looking at the function signature. Certainly I might not know how to handle all the error variations, but not having to decide if I need to wrap something in try/catch... I dunno it just resonates with me.