-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_contain.go
112 lines (100 loc) · 2.4 KB
/
array_contain.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2021 Hollson. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gdk
import (
"errors"
"reflect"
)
// 目标元素tar是否包含在container集合中
func ContainByte(tar byte, container ...byte) bool {
for _, v := range container {
if v == tar {
return true
}
}
return false
}
// 目标元素tar是否包含在container集合中
func ContainInt(tar int, container ...int) bool {
for _, v := range container {
if v == tar {
return true
}
}
return false
}
// 目标元素tar是否包含在container集合中
func ContainInt32(tar int32, container ...int32) bool {
for _, v := range container {
if v == tar {
return true
}
}
return false
}
// 目标元素tar是否包含在container集合中
func ContainInt64(tar int64, container ...int64) bool {
for _, v := range container {
if v == tar {
return true
}
}
return false
}
// 目标元素tar是否包含在container集合中
func ContainFloat32(tar float32, container ...float32) bool {
for _, v := range container {
if v == tar {
return true
}
}
return false
}
// 目标元素tar是否包含在container集合中
func ContainFloat64(tar float64, container ...float64) bool {
for _, v := range container {
if v == tar {
return true
}
}
return false
}
// 目标元素tar是否包含在container集合中
func ContainString(tar string, container ...string) bool {
for _, v := range container {
if tar == v {
return true
}
}
return false
}
// 目标元素tar是否包含在container集合中,container必须是切片类型
// 如:Contain(1,[]int{1,2,3})
// Contain(u,[]User{u1,u2,u3})
// Contain(&u,[]*User{&u1,&u2,&u3})
// Contain(1.0, []interface{}{1.0,"a",'b'})
func Contain(tar interface{}, container interface{}) (bool, error) {
if arr, err := Convert2AnyTypeSlice(container); err!=nil {
return contain(tar, arr), nil
}
return false, errors.New("container is not a slice type")
}
// 同Contain,但需手动处理panic
func ContainMust(tar interface{}, container interface{}) bool {
b, err := Contain(tar, container)
if err != nil {
panic(err)
}
return b
}
func contain(tar interface{}, container []interface{}) bool {
a := reflect.ValueOf(tar)
for _, v := range container {
if a.Interface() == v {
return true
}
// reflect.DeepEqual(a.Interface(), reflect.ValueOf(v))
}
return false
}