-
Notifications
You must be signed in to change notification settings - Fork 3
/
isPointer.go
executable file
·50 lines (43 loc) · 1.04 KB
/
isPointer.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
package ptrKit
import (
"fmt"
"github.com/modern-go/reflect2"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"reflect"
)
// IsPointer 传参的类型是否为指针?
/*
PS: 传参的值无影响,即使其为nil.
@param obj 可以为nil
e.g.
type bean struct {
}
var b *bean = nil
fmt.Println(ptrKit.IsPointer(b)) // true(类型为指针,虽然值为nil)
fmt.Println(ptrKit.IsPointer(interface{}(nil))) // false
*/
func IsPointer(obj interface{}) bool {
// 参数obj为nil的话,变量v不为nil
v := reflect.ValueOf(obj)
return v.Kind() == reflect.Ptr
}
// IsPointer1
/**
* 判断参数obj是否为指针(pointer).
* 参考:github.com/json-iterator/go => reflect.go => func (iter *Iterator) ReadVal(obj interface{})
*
* @param obj nil => false
*/
func IsPointer1(obj interface{}) bool {
typ := reflect2.TypeOf(obj)
return typ != nil && typ.Kind() == reflect.Ptr
}
// IsPointer2
/*
e.g.
(nil) => false
*/
func IsPointer2(obj interface{}) bool {
str := fmt.Sprintf("%T", obj)
return strKit.StartWith(str, "*")
}