first commit

This commit is contained in:
xuthus5 2023-06-22 01:55:03 +08:00
commit 9bee051e56
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
4 changed files with 156 additions and 0 deletions

11
README.md Normal file
View File

@ -0,0 +1,11 @@
## goref
一个interface反射库
## 类型断言方法
- IsSlice 是否是slice
- IsArray 是否是array
- ...
## 其他方法

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitter.top/coco/goref
go 1.20

1
goref.go Normal file
View File

@ -0,0 +1 @@
package goref

141
type.go Normal file
View File

@ -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
}