interface类型的比较nil == nil 分为类型和值进行比较,只有类型相同,值为nil时才为nil

This commit is contained in:
朱毅骏 2021-07-12 14:21:13 +08:00
parent 7b16829151
commit 42fce35085
2 changed files with 49 additions and 0 deletions

24
problem/problem1.go Normal file
View File

@ -0,0 +1,24 @@
package main
import "log"
type MyErr struct {
Msg string
}
//interface的比较nil 分为类型和值进行比较
func main() {
//var e error //false
var e *MyErr //true
e = GetErr()
log.Println(e == nil)
}
func GetErr() *MyErr {
return nil
}
func (m *MyErr) Error() string {
return "脑子进煎鱼了"
}

25
problem/problem2.go Normal file
View File

@ -0,0 +1,25 @@
package main
import "log"
//interface的比较nil 分为类型和值进行比较
type Base interface {
do()
}
type App struct {
}
func main() {
//var base Base //false
var base *App //true
base = GetApp()
log.Println(base)
log.Println(base == nil)
}
func GetApp() *App {
return nil
}
func (a *App) do() {}