假设我有自定义错误类型MyError
:
type MyError struct{}
func (me *MyError) Error() string { return fmt.Sprintf("Error() called on %#v", me) }
某些可能会返回该类型错误的函数:
func SomeFunc() *MyError {
return nil
}
为什么世界上err != nil
对于零错误都是正确的?:
var err error
err = SomeFunc()
if err != nil {
fmt.Printf("err != nil, but err is actually nil: %#v", err)
}
// the above prints: "err != nil, but err is actually nil: (*main.MyError)(nil)"
将nil指针指定给接口类型的变量...
,这有点奇怪