3 ms·
Here's one of my favorite gotchas in Go: why does this error? https://play.golang.org/p/6LTbtuocu5- https://play.golang.org/p/6LTbtuocu5-
by hombre_fatal 8y ago
Here's one of my favorite gotchas in Go: why does this error?
https://play.golang.org/p/6LTbtuocu5- https://play.golang.org/p/6LTbtuocu5-
- nemothekid 8y agonil interfaces is one of the strangest things in language, and after learning Rust after using Go for years, I think not having Option types is the real wart in the language. I've never really cared about Generics, but having nullable types is a real mistake to me.
- DaiPlusPlus 8y ago> but having nullable types is a real mistake to me. Nullable types by themselves are fine, the language and the compiler just needs to make handling “is X null?” work the same way as option types do - or better yet: languages were a variable is only in scope if it is not null.
- deleted 8y ago[deleted]
- hombre_fatal 8y agoYeah, nullable types are one thing. Go's null interface is another: https://play.golang.org/p/N_3BiUOkRJo https://play.golang.org/p/N_3BiUOkRJo It's probably the first and last to have this quirk.
- echlebek 8y agoIt errors because the type of `err` is `error`, not *formatError. That's why you are supposed to return the error interface, instead of a concrete type. The value ends up being a non-nil interface value that holds nil. To avoid encountering this issue, return `error`, not something else. https://play.golang.org/p/jZ5Fa24bbUz https://play.golang.org/p/jZ5Fa24bbUz
- chrsm 8y agoThe real issue here is that it is not immediately obvious that this is actually an interface - one expects the second `err` to be *FormatError due to the signature - it happens to implement the error interface so it goes unnoticed. Shadowing in a different scope allows it to work properly as well. I admit that I've never actually run in to this issue in practice as I generally return custom errors as `error`, but I can see why it would confuse someone.
- echlebek 8y agoTotes.
- deleted 8y ago[deleted]
- hombre_fatal 8y ago> To avoid encountering this issue, return `error`, not something else. But best practice is to return concrete types. Also, `err` is just an example in this minimal snippet. It can happen with any variable. Here's a similar snippet: https://play.golang.org/p/WKPey_PL_ht https://play.golang.org/p/WKPey_PL_ht -- Looks impossible to crash. :P Spoiler: it's a boxed null, not a null.
- thegeekpirate 8y ago> But best practice is to return concrete types. You're taking this as an absolute when you shouldn't be. There's plenty of places in the standard library that don't follow this "rule".
- deleted 8y ago[deleted]
- chrsm 8y agoBest practice is to return concrete types for everything, but error is one of those "special" cases that people tend to return an `error` for, IME. You can always unwrap it via type assertion, however ugly that may be.
- pokstad 8y agoI believe this is a quirk of how interfaces are implemented: https://golang.org/doc/faq#nil_error https://golang.org/doc/faq#nil_error