Browse Source

添加反射函数

yjp 6 months ago
parent
commit
a8b422a6b6
2 changed files with 8 additions and 0 deletions
  1. 4 0
      reflectutils/type.go
  2. 4 0
      reflectutils/value.go

+ 4 - 0
reflectutils/type.go

@@ -36,6 +36,10 @@ func SliceTypeElem(t reflect.Type) reflect.Type {
 	return elem
 }
 
+func IsSliceTypeOf(t reflect.Type, elementKind reflect.Kind) bool {
+	return t.Kind() == reflect.Slice && t.Elem().Kind() == elementKind
+}
+
 func IsTypeInteger(t reflect.Type) bool {
 	return t.Kind() >= reflect.Int && t.Kind() <= reflect.Int64
 }

+ 4 - 0
reflectutils/value.go

@@ -40,6 +40,10 @@ func SliceValueElem(v reflect.Value) reflect.Value {
 	return elem
 }
 
+func IsSliceValueOf(v reflect.Type, elementKind reflect.Kind) bool {
+	return v.Kind() == reflect.Slice && v.Elem().Kind() == elementKind
+}
+
 func IsValueInteger(v reflect.Value) bool {
 	return v.Kind() >= reflect.Int && v.Kind() <= reflect.Int64
 }