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.
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.
Go also has an 'unsafe' package that can mess things up at runtime--don't do that and don't ignore errors.
https://github.com/golang/go/blob/master/src/cmd/go/internal... https://github.com/golang/go/blob/master/src/io/fs/fs.go#L26... https://github.com/golang/go/blob/master/src/errors/wrap.go#... https://github.com/golang/go/blob/master/src/go/parser/resol...
If `ok' gets set to false, that seems optimal. Pretty neat, in fact, what other language permits such low-key, easy runtime interface checks?
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
An example is in the errors package: https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/...