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

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

判断规则:

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

nil 类型全部视为空

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

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

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

go代码实现

Go 复制代码
import (
	"reflect"
)

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

// 	判断规则:
// 	bool类型因为只有true和false 所以 全部视为非空
// 	nil 类型全部视为空
// 	所有数字类型的 零值全部视为空
// 	对应指针类型数据,只要是非nil 则视为非空
// 	对于时间类型结构体或者其他带有 IsZero()方法的结构体 动态调用IsZero()方法来判断是否为空
//
// @author: tekintian <tekintian@gmail.com>
// @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)
		}
	}
}
相关推荐
栈与堆5 分钟前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
一路向北·重庆分伦7 分钟前
03-01:MQ常见问题梳理
java·开发语言
txinyu的博客12 分钟前
结合游戏场景理解,互斥锁,读写锁,自旋锁,CAS / 原子变量,分段锁
开发语言·c++·游戏
阿里嘎多学长20 分钟前
2026-01-11 GitHub 热点项目精选
开发语言·程序员·github·代码托管
yuanyikangkang21 分钟前
STM32 lin控制盒
开发语言
txinyu的博客1 小时前
HTTP服务实现用户级窗口限流
开发语言·c++·分布式·网络协议·http
代码村新手1 小时前
C++-类和对象(上)
开发语言·c++
zh_xuan1 小时前
kotlin 类继承的语法
开发语言·kotlin
全栈小精灵1 小时前
Winform入门
开发语言·机器学习·c#