commit 9bee051e56b2a08280e536ba1fa47fbc4bc44455 Author: xuthus5 Date: Thu Jun 22 01:55:03 2023 +0800 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..9d30618 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +## goref + +一个interface反射库 + +## 类型断言方法 + +- IsSlice 是否是slice +- IsArray 是否是array +- ... + +## 其他方法 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f01554b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitter.top/coco/goref + +go 1.20 diff --git a/goref.go b/goref.go new file mode 100644 index 0000000..95b04a5 --- /dev/null +++ b/goref.go @@ -0,0 +1 @@ +package goref diff --git a/type.go b/type.go new file mode 100644 index 0000000..a1b4dc3 --- /dev/null +++ b/type.go @@ -0,0 +1,141 @@ +package goref + +import "reflect" + +func IsInt(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Int +} + +func IsInt8(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Int8 +} + +func IsInt16(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Int16 +} + +func IsInt32(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Int32 +} + +func IsInt64(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Int64 +} + +func IsUint(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Uint +} + +func IsUint8(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Uint8 +} + +func IsUint16(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Uint16 +} + +func IsUint32(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Uint32 +} + +func IsUint64(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Uint64 +} + +func IsInteger(val any) bool { + typeKind := reflect.TypeOf(val).Kind() + return typeKind >= reflect.Int && typeKind <= reflect.Uint64 +} + +func IsFloat32(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Float32 +} + +func IsFloat64(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Float64 +} + +func IsFloat(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Float32 || typeOf.Kind() == reflect.Float64 +} + +func IsNumber(val any) bool { + return IsInteger(val) || IsFloat(val) +} + +func IsBool(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Bool +} + +func IsString(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.String +} + +func IsArray(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Array +} + +func IsSlice(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Slice +} + +func IsList(val any) bool { + return IsArray(val) || IsSlice(val) +} + +func IsMap(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Map +} + +func IsStruct(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Struct +} + +func IsInterface(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Interface +} + +func IsFunc(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Func +} + +func IsPointer(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Pointer +} + +func IsUnsafePointer(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.UnsafePointer +} + +func IsUintptr(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Uintptr +} + +func IsChannel(val any) bool { + typeOf := reflect.TypeOf(val) + return typeOf.Kind() == reflect.Chan +}