-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
60 lines (50 loc) · 1.28 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
package main
import (
"log"
"math/rand"
"net/url"
"time"
)
// Seed
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
// String charset to randomly pick from
const RandomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// Return a random string of the given length
func getRandString(length int) string {
b := make([]byte, length)
for i := range b {
b[i] = RandomStringCharset[seededRand.Intn(len(RandomStringCharset))]
}
return string(b)
}
// Get the elapsed milliseconds from a given starting point
func getElapsedTimeInMS(start int64) int64 {
return (time.Now().UnixNano() - start) / int64(time.Millisecond)
}
// isValidUrl tests a string to determine if it is a well-structured url or not.
func isValidUrl(toTest string) (*url.URL, bool) {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return nil, false
}
u, err := url.Parse(toTest)
if err != nil || u.Scheme == "" || u.Host == "" {
return nil, false
}
return u, true
}
// Contains a string in a slice
func contains(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
// If a critical error pops up, fail
func checkFatalError(err error) {
if err != nil {
log.Fatal(err)
}
}