go 判断结构体是否为空
文章目录
package main
import (
"fmt"
"reflect"
)
type A struct{
name string
age int
}
func (a A) IsEmpty() bool {
return reflect.DeepEqual(a, A{})
}
func main() {
var a A
if a == (A{}) { // 括号不能去
fmt.Println("a == A{} empty")
}
if a.IsEmpty() {
fmt.Println("reflect deep is empty")
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论