3 ms·
It's no surprise. The example is ignoring the error from casting. It should be: if quacker, ok := duck.(interface { Quack() }); ok { quacker.Quack() }
by karmakaze 2y ago
It's no surprise. The example is ignoring the error from casting. It should be:
if quacker, ok := duck.(interface { Quack() }); ok {
quacker.Quack()
}
Ignoring errors leads to unexpected results.
Failing to mention that it's purposely ignoring an error for a fun blog post is the opposite of informative.
- 38 2y agothat an who the fuck uses interface literals? I do Go personally and professionally for years and never seen it.
- didip 2y agoI was about to say, after so many years doing Go, I have never seen this and would love to read blog post about why interface literal is useful.
- mseepgood 2y agoYou can learn from the Go codebase to get a feel of why this is useful: https://github.com/golang/go/blob/master/src/cmd/go/internal/modfetch/repo.go#L394 https://github.com/golang/go/blob/master/src/cmd/go/internal... https://github.com/golang/go/blob/master/src/io/fs/fs.go#L262 https://github.com/golang/go/blob/master/src/io/fs/fs.go#L26... https://github.com/golang/go/blob/master/src/errors/wrap.go#L18 https://github.com/golang/go/blob/master/src/errors/wrap.go#... https://github.com/golang/go/blob/master/src/go/parser/resolver.go#L49 https://github.com/golang/go/blob/master/src/go/parser/resol...
- metadat 2y agoIf the interface check fails, does the code panic or does the `ok' value just get set to false? If `ok' gets set to false, that seems optimal. Pretty neat, in fact, what other language permits such low-key, easy runtime interface checks?
- 38 2y agothis will panic: v := hello.(world) this never panic: v, ok := hello.(world) and yes, its awesome. people rag on Go because BLARG ERRORS BOILERPLATE, but if you can get over that Go is not Python/JavaScript, you're left with a simple powerful language
- mariusor 2y agoThe Go standard library uses it. I didn't suss out the reason for it though. An example is in the errors package: https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/errors/wrap.go;l=58 https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/...
- jayd16 2y agoBut it mentions exactly this after the page break?
- karmakaze 2y agoRight it mentions it when casting to Duck. The post makes it read like casting to `interface { Quack() }` was a problem, when really it was the ignoring of the error return value from that cast.
- vips7L 2y agoThe language shouldn’t let you silently ignore errors.
- akira2501 2y agoIt's not silent. It will panic.
- candleknight 2y agobut that'll be at runtime
- mplewis 2y agoIn Go, a runtime panic indicates a programmer error. This means you should not have written that line of code. The solution is to fix the line of code.
- Thaxll 2y agoEvery languages let you ignore errors, Rust included.
- nhumrich 2y agoThe fact that the error happens _at run time_ is exactly the point. Whether the error is handled or not has nothing to do with the point of the post.