func isEmpty(x interface{}) bool {
rt := reflect.TypeOf(x)
if rt == nil {
return true
}
rv := reflect.ValueOf(x)
switch rv.Kind() {
case reflect.Array, reflect.Map, reflect.Slice:
return rv.Len() == 0
}
return reflect.DeepEqual(x, reflect.Zero(rt).Interface())
}
Golang 判断空值函数代码
最新推荐文章于 2025-02-07 15:44:44 发布
本文详细解释了Go语言中的isEmpty函数,该函数通过`reflect`包判断接口类型的值是否为空,涉及Kind判断和零值比较。
407

被折叠的 条评论
为什么被折叠?



