-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
63 lines (50 loc) · 1.15 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
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"math/rand"
"net/url"
"time"
)
func Some[T any](arr []T, f func(T) bool) bool {
for _, t := range arr {
if f(t) {
return true
}
}
return false
}
func RandDurInRange(minDur, maxDur time.Duration) time.Duration {
if minDur > maxDur {
minDur, maxDur = maxDur, minDur
}
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
randomMillis := r.Int63n(int64(maxDur - minDur))
return minDur + time.Duration(randomMillis)
}
func stripQueryString(u string) (string, error) {
parsedUrl, err := url.Parse(u)
if err != nil {
return "", err
}
parsedUrl.RawQuery = ""
return parsedUrl.String(), nil
}
func PrintWithTimestamp(s string) {
timestamp := time.Now().Format(time.UnixDate)
fmt.Printf("[%s] %s\n\n", timestamp, s)
}
func PrintWithTimestampf(format string, a ...any) {
PrintWithTimestamp(fmt.Sprintf(format, a...))
}
func PrintErr(err error) {
PrintWithTimestampf("Error: %s", err.Error())
}
func PrintErrWithTimeout(err error, timeout time.Duration) {
PrintErr(err)
Sleep(timeout)
}
func Sleep(d time.Duration) {
PrintWithTimestampf("Sleeping for %f seconds", d.Seconds())
time.Sleep(d)
}