-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.go
52 lines (46 loc) · 905 Bytes
/
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
package logpeck
import (
"errors"
"math/rand"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
)
// LogExecTime .
func LogExecTime(start time.Time, prefix string) {
elapsedMs := time.Since(start) / time.Millisecond
log.Debugf("Performance: %s cost %d ms.", prefix, elapsedMs)
}
// GetHost .
func GetHost() string {
host, err := os.Hostname()
if err != nil {
panic(err)
}
return host
}
// SelectRandom .
func SelectRandom(candidates []string) (string, error) {
l := len(candidates)
if l <= 0 {
return "", errors.New("none candidates")
}
ret := candidates[rand.Intn(l)]
return ret, nil
}
// SplitString .
func SplitString(content, delims string) []string {
if len(delims) == 0 {
delims = "\t\r\n "
}
splitFunc := func(r rune) bool {
for _, d := range delims {
if r == d {
return true
}
}
return false
}
return strings.FieldsFunc(content, splitFunc)
}