golang中空值判断函数,支持任意类型的空值判断

使用反射方式对any任意类型的数据是否为空判断, 可判断时间对象是否为空, 可判断所有数字类型,指针类型和结构体字符串是否为空

判断规则:

bool类型因为只有true和false 所以 全部视为非空

nil 类型全部视为空

所有数字类型的 零值全部视为空

对应指针类型数据,只要是非nil 则视为非空

对于时间类型结构体或者其他带有 IsZero()方法的结构体 动态调用IsZero()方法来判断是否为空

go代码实现

Go 复制代码
import (
	"reflect"
)

// 使用反射方式对any任意类型的数据是否为空判断, 可判断时间对象是否为空, 可判断所有数字类型,指针类型和结构体字符串是否为空

// 	判断规则:
// 	bool类型因为只有true和false 所以 全部视为非空
// 	nil 类型全部视为空
// 	所有数字类型的 零值全部视为空
// 	对应指针类型数据,只要是非nil 则视为非空
// 	对于时间类型结构体或者其他带有 IsZero()方法的结构体 动态调用IsZero()方法来判断是否为空
//
// @author: tekintian <[email protected]>
// @see https://dev.tekin.cn
func IsEmpty(data interface{}) bool {
	rv := reflect.ValueOf(data)
	rvk := rv.Kind()
	switch rvk {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
		return rv.IsZero()
	case reflect.Uintptr, reflect.Map, reflect.Slice, reflect.Pointer, reflect.UnsafePointer:
		return rv.IsNil() // 指针类型数据全部调用 IsNil() 进行判断,非nil的指针视为非空
	case reflect.String:
		return rv.Len() == 0
	case reflect.Struct:
		if rv.MethodByName("IsZero").IsValid() {
			// 动态调用结构体中的 IsZero 方法
			rt := rv.MethodByName("IsZero").Call([]reflect.Value{}) // call后面的参数即是要调用方法的参数 []reflect.Value{} 表示无参数
			return len(rt) > 0 && rt[0].Bool()                      // IsZero方法的返回值只有一个bool,
		} else {
			// 对值的零值类型进行深度比较
			return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
		}
	case reflect.Invalid: // nil值的reflect类型就是 Invalid
		return true // nil也算是空值
	}

	return false // 其他情况默认非空
}

单元测试用例

Go 复制代码
import (
	"gotms/utils"
	"testing"
	"time"
)

type DemoSt struct {
	Name    string
	Items   []DemoItem
	Data    interface{}
	Created time.Time
}
type DemoItem struct {
	Name string
}

func TestIsEmpty(t *testing.T) {
	cases := []struct {
		input    interface{}
		expected bool
	}{
		{input: time.Now(), expected: false},
		{input: DemoSt{}.Created, expected: true}, // 空时间对象 这个是未初始化的时间对象 视为空
		{input: time.Time{}, expected: true},      // 空时间对象 已经初始化
		{input: false, expected: false},
		{input: DemoSt{}.Items, expected: true}, // 这里的DemoSt{}.Items 是一个不可达的对象,即未初始化的对象 视为空
		{input: DemoSt{}.Data, expected: true},  // nil
		{input: "", expected: true},
		{input: 0.0, expected: true},
		{input: nil, expected: true},
		{input: complex(0, 0), expected: true}, // 复数的判断
		{input: complex(2, 1), expected: false},
	}
	for _, v := range cases {
		ret := utils.IsEmpty(v.input)
		if ret != v.expected {
			t.Fatalf("%v test failed: expected: %v, got: %v", v.input, v.expected, ret)
		}
	}
}
相关推荐
陈皮话梅糖@43 分钟前
使用 Provider 和 GetX 实现 Flutter 局部刷新的几个示例
开发语言·javascript·flutter
hvinsion2 小时前
基于PyQt5的自动化任务管理软件:高效、智能的任务调度与执行管理
开发语言·python·自动化·自动化任务管理
Aphelios3802 小时前
Java全栈面试宝典:线程机制与Spring IOC容器深度解析
java·开发语言·jvm·学习·rbac
qq_529835352 小时前
装饰器模式:如何用Java打扮一个对象?
java·开发语言·装饰器模式
日暮南城故里2 小时前
Java学习------源码解析之StringBuilder
java·开发语言·学习·源码
Vitalia3 小时前
从零开始学Rust:枚举(enum)与模式匹配核心机制
开发语言·后端·rust
双叶8363 小时前
(C语言)虚数运算(结构体教程)(指针解法)(C语言教程)
c语言·开发语言·数据结构·c++·算法·microsoft
一个public的class5 小时前
什么是 Java 泛型
java·开发语言·后端
士别三日&&当刮目相看5 小时前
JAVA学习*Object类
java·开发语言·学习
invincible_Tang5 小时前
R格式 (15届B) 高精度
开发语言·算法·r语言