-
Notifications
You must be signed in to change notification settings - Fork 0
/
goutils.go
145 lines (141 loc) · 2.77 KB
/
goutils.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package goutils
import (
"crypto/sha1"
"database/sql"
"encoding/base64"
"fmt"
"html/template"
"io"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
)
//puts path vars right into variables passed as params, until it runs out
//ex: pathVars(r,"/entry/",&id,&todo,&val) // populates id, todo, and val
func PathVars(r *http.Request, root string, vals ...*string) {
a := strings.Split(r.URL.Path[len(root):], "/")
for i := range vals {
if len(a) > i {
*vals[i] = a[i]
} else {
*vals[i] = ""
}
}
}
func Sth(db *sql.DB, s string) (a *sql.Stmt, err error) {
a, err = db.Prepare(s)
if err != nil {
fmt.Println(err)
err.Error()
}
return a, err
}
func Average(inputs interface{})(a float64) {
var sum float64
if f, ok := inputs.([]float64); ok {
for _, i := range f {
sum = sum + float64(i)
}
return sum / float64(len(f))
}
if f, ok := inputs.([]float32); ok {
for _, i := range f {
sum = sum + float64(i)
}
return sum / float64(len(f))
}
if f, ok := inputs.([]int); ok {
for _, i := range f {
sum = sum + float64(i)
}
return sum / float64(len(f))
}
return a
}
func Evenodd(i int) string {
if i%2 == 0 {
return "even"
}
return "odd"
}
func Tostr(i interface{}) string {
switch t := i.(type) {
case string:
return i.(string)
case int:
return strconv.Itoa(i.(int))
case bool:
return strconv.FormatBool(i.(bool))
case template.HTML:
return string(i.(template.HTML))
case []byte:
return string(i.([]byte))
default:
t = t
}
return i.(string)
}
func Toint(s string) int {
i, _ := strconv.Atoi(s)
return i
}
func Shuffle(data interface{}) {
switch t := interface{}(data).(type) {
case *[]int:
a := data.(*[]int)
data = Shufflei(*a)
case *[]string:
a := data.(*[]string)
data = Shuffles(*a)
default:
fmt.Printf("unexpected type %T", t)
}
}
func Shuffles(slc []string) []string {
rand.Seed(time.Now().UnixNano())
N := len(slc)
for i := 0; i < N; i++ {
r := i + rand.Intn(N-i)
slc[r], slc[i] = slc[i], slc[r]
}
return slc
}
func Shufflei(slc []int) []int {
rand.Seed(time.Now().UnixNano())
N := len(slc)
for i := 0; i < N; i++ {
// choose index uniformly in [i, N-1]
r := i + rand.Intn(N-i)
slc[r], slc[i] = slc[i], slc[r]
}
return slc
}
func GetHash(s string) string {
h := sha1.New()
io.WriteString(h, s)
return fmt.Sprintf("%s", base64.URLEncoding.EncodeToString(h.Sum(nil)))
}
func RandomString(l int) string {
rand.Seed(time.Now().UTC().UnixNano())
bytes := make([]byte, l)
for i := 0; i < l; i++ {
bytes[i] = byte(RandInt(65, 90))
}
return string(bytes)
}
func RandInt(min int, max int) int {
rand.Seed(time.Now().UnixNano())
return min + rand.Intn(max-min)
}
func Ef(err error) {
if err != nil {
panic(err.Error())
}
}
func Ew(err error) {
if err != nil {
print("WARN: " + err.Error())
}
}