|
|
|
|
|
by im-a-baby
1328 days ago
|
|
Yea, the issue is that nil's in Go are typed. So `err == nil` is checking err against error's nil value, but the value stored in err is *MyError's nil value. package main
import "fmt"
type MyError struct{}
func (m *MyError) Error() string {
return ""
}
func main() {
var err error = nil
var myErr *MyError = nil
fmt.Println(err == myErr) // this is false
}
|
|
Normally one would not compare two interface values, so this issue really comes up when comparing an interface to nil. If Go had proper option types then this issue would go away.
https://go101.org/article/nil.html
package main
import "fmt"
type Err1 struct{}
func (m Err1) Error() string { return "" }
type Err2 struct{}
func (m Err2) Error() string { return "" }
func main() {