-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
54 lines (50 loc) · 1.05 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
package tcpx
import (
"strings"
)
// Whether s in arr
// Support %%
func In(s string, arr []string) bool {
for _, v := range arr {
if strings.Contains(v, "%") {
if strings.HasPrefix(v, "%") && strings.HasSuffix(v, "%") {
if strings.Contains(s, string(v[1:len(v)-1])) {
return true
}
} else if strings.HasPrefix(v, "%") {
if strings.HasSuffix(s, string(v[1:])) {
return true
}
} else if strings.HasSuffix(v, "%") {
if strings.HasPrefix(s, string(v[:len(v)-1])) {
return true
}
}
} else {
if v == s {
return true
}
}
}
return false
}
// Defer eliminates all panic cases and handle panic reason by handlePanicError
func Defer(f func(), handlePanicError ...func(interface{})) {
defer func() {
if e := recover(); e != nil {
for _, handler := range handlePanicError {
handler(e)
}
}
}()
f()
}
// CloseChanel(func(){close(chan)})
func CloseChanel(f func()) {
defer func() {
if e := recover(); e != nil {
// when close(chan) panic from 'close of closed chan' do nothing
}
}()
f()
}