File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ package types
2
+
3
+ import (
4
+ "reflect"
5
+ )
6
+
7
+ // IsPrimitiveType return whether type of v is primitive type or not.
8
+ func IsPrimitiveType (v interface {}) bool {
9
+ rt := reflect .TypeOf (v )
10
+ if rt .Kind () == reflect .Ptr {
11
+ rt = rt .Elem ()
12
+ }
13
+ k := rt .Kind ()
14
+ return k == reflect .Int || k == reflect .Int8 || k == reflect .Int16 || k == reflect .Int32 || k == reflect .Int64 ||
15
+ k == reflect .Uint || k == reflect .Uint8 || k == reflect .Uint16 || k == reflect .Uint32 || k == reflect .Uint64 || k == reflect .Uintptr ||
16
+ k == reflect .Complex64 || k == reflect .Complex128 ||
17
+ k == reflect .Float32 || k == reflect .Float64 || k == reflect .Bool || k == reflect .String
18
+ }
19
+
20
+ // ZeroValue generate zero value of given data.
21
+ // For example:
22
+ // give int 8 will generate int 0
23
+ // give string "hello" will generate string ""
24
+ // give struct User{name: "lily", age: 123} will generate struct User{}
25
+ func ZeroValue (v interface {}) interface {} {
26
+ return reflect .Zero (reflect .TypeOf (v )).Interface ()
27
+ }
28
+
29
+ // IsSameType check a's runtime type equal b's runtime type
30
+ func IsSameType (a interface {}, b interface {}) bool {
31
+ ra := reflect .TypeOf (a )
32
+ pa := false
33
+ if ra .Kind () == reflect .Ptr {
34
+ ra = ra .Elem ()
35
+ pa = true
36
+ }
37
+ rb := reflect .TypeOf (b )
38
+ pb := false
39
+ if rb .Kind () == reflect .Ptr {
40
+ rb = rb .Elem ()
41
+ pb = true
42
+ }
43
+ return pa == pb && ra .Kind () == rb .Kind ()
44
+ }
You can’t perform that action at this time.
0 commit comments