Can someone explain this go code to me?
When this is invoked, the err != nil
returns true, thus leading to a nil pointer reference.
type E struct{}
func (e E) Error() string {
return ""
}
func DoStuff() *E {
return nil
}
func main() {
var err *E
err = DoStuff()
log.Println(err) // nil
testErr(err)
}
func testErr(err error) {
if err != nil {
log.Println("got error", err.Error())
}
}
https://play.golang.org/p/iys7U_UMhG
I have understood that this has something to do with the fact that I am dealing with a nil-struct-pointer casted to an interface...
However I am confused how to design this interface DoStuff
, if one wants to return a "smart" error object, not just plain error
.