-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
78 lines (64 loc) · 1.93 KB
/
reflect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package slice
import (
"reflect"
"unsafe"
)
// 反射相关的操作
// 访问slice的元数据
// 通过指针直接访问等
// ---------------------------------------------------------------------------------------------------------------------
// IsSliceType 判断是否是切片类型
func IsSliceType(v any) bool {
of := reflect.TypeOf(v)
if of == nil {
return false
}
return of.Kind() == reflect.Slice
}
// IsArrayType 判断值是否是数组类型
func IsArrayType(v any) bool {
of := reflect.TypeOf(v)
if of == nil {
return false
}
return of.Kind() == reflect.Array
}
// IsSliceOrArrayType 判断是是否是切片或者数组类型
func IsSliceOrArrayType(v any) bool {
of := reflect.TypeOf(v)
if of == nil {
return false
}
return of.Kind() == reflect.Slice || of.Kind() == reflect.Array
}
// ---------------------------------------------------------------------------------------------------------------------
// IsSliceValue 判断存储的值是否是切片
func IsSliceValue(v any) bool {
of := reflect.ValueOf(v)
if !of.IsValid() {
return false
}
return of.Kind() == reflect.Slice
}
// IsArrayValue 判断存储的值是否是数组
func IsArrayValue(v any) bool {
of := reflect.ValueOf(v)
if !of.IsValid() {
return false
}
return of.Kind() == reflect.Array
}
// IsSliceOrArrayValue 判断存储的值是否是切片或数组
func IsSliceOrArrayValue(v any) bool {
of := reflect.ValueOf(v)
if !of.IsValid() {
return false
}
return of.Kind() == reflect.Slice || of.Kind() == reflect.Array
}
// ---------------------------------------------------------------------------------------------------------------------
// GetSliceHeader 通过反射获取切片的元数据信息
func GetSliceHeader[T any](slice []T) *reflect.SliceHeader {
return (*reflect.SliceHeader)(unsafe.Pointer(&slice))
}
// ---------------------------------------------------------------------------------------------------------------------