|
|
|
|
|
by gregwebs
1323 days ago
|
|
Comparing different types normally is a type error. But for interfaces the non-interface value will be converted to the type of the interface value. This conversion gives it a boxed dynamic type. This is different type than just assigning nil, which is the untyped nil, which sets the interface to its zero value. 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() { var err error = nil
var myErr1 *Err1 = nil
var myErr2 *Err2 = nil
fmt.Println(err == myErr1) // this is false
fmt.Println(err == myErr2) // this is false
fmt.Println((error)(myErr1) == (error)(myErr2)) // this is false. requires the cast to do the comparison
}
|
|