-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathutil.go
55 lines (45 loc) · 1.05 KB
/
util.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
package common
import (
"math/rand"
"time"
)
////////////////////////////////////////////////////////////////////////////////
func Find(slice []string, value string) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
func Remove(slice []string, value string) []string {
for i, v := range slice {
if v == value {
return append(slice[:i], slice[i+1:]...)
}
}
return slice
}
func RandomShuffle(slice []string) {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(
len(slice),
func(i, j int) { slice[i], slice[j] = slice[j], slice[i] },
)
}
func RandomDuration(min time.Duration, max time.Duration) time.Duration {
rand.Seed(time.Now().UnixNano())
x := min.Microseconds()
y := max.Microseconds()
if y <= x {
return min
}
return time.Duration(x+rand.Int63n(y-x)) * time.Microsecond
}
func RandomElement(slice []string) string {
rand.Seed(time.Now().UnixNano())
return slice[rand.Intn(len(slice))]
}
func WaitForRandomDuration(min time.Duration, max time.Duration) {
time.Sleep(RandomDuration(min, max))
}