-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (78 loc) · 1.62 KB
/
utils.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
package utils
import (
"bytes"
"regexp"
"strings"
"reflect"
)
//检查字符串,去掉特殊字符
func CheckCharDoSpecial(s string, char byte, regs string) string {
reg := regexp.MustCompile(regs)
var result []string
if arr := reg.FindAllString(s, -1); len(arr) > 0 {
buf := bytes.Buffer{}
for key, val := range arr {
if val != string(char) {
buf.WriteString(val)
}
if val == string(char) && buf.Len() > 0 {
result = append(result, buf.String())
buf.Reset()
}
//处理最后一批数据
if buf.Len() > 0 && key == len(arr)-1 {
result = append(result, buf.String())
}
}
}
return strings.Join(result, string(char))
}
// 判断某一个值是否含在切片之中
func InArray(need interface{}, haystack interface{}) bool {
switch key := need.(type) {
case int:
for _, item := range haystack.([]int) {
if item == key {
return true
}
}
case string:
for _, item := range haystack.([]string) {
if item == key {
return true
}
}
case int64:
for _, item := range haystack.([]int64) {
if item == key {
return true
}
}
case float64:
for _, item := range haystack.([]float64) {
if item == key {
return true
}
}
default:
return false
}
return false
}
//判断某一个值是否含在切片之中
func In_Array(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}
return
}