自定义的结构体类型,并不是简单的与 nil 做比较。
package main
import(
"fmt"
)
type Person struct{
Name string
Age int
}
func main(){
var one Person
one.Name = "xiaoming"
one.Age = 12
var two Person
if one != (Person{}) {
fmt.Println(one.Name, "的年龄是", one.Age)
} else{
fmt.Println("the person is nil")
}
if two != (Person{}) {
fmt.Println(two.Name, "的年龄是", two.Age)
} else{
fmt.Println("the person is nil")
}
}
代码结果:
xiaoming 的年龄是 12
the person is nil
运行结果截图:
如果放开上面代码的注释,编译器会提示如下错误信息:
运行结果截图: