-
Notifications
You must be signed in to change notification settings - Fork 124
/
util.go
61 lines (53 loc) · 1.09 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
package gorose
import (
"database/sql"
"github.com/gohouse/gorose/v3/builder"
"math/rand"
"time"
)
func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
}
func As(table any, alias string) builder.TableClause {
return builder.TableClause{
Tables: table,
Alias: alias,
}
}
func GetRandomInt(num int) int {
return rand.Intn(num)
}
//func GetRandomWeightedIndex(weights []int) int {
// if len(weights) == 0 {
// return 0
// }
// if len(weights) == 1 {
// return 0
// }
// totalWeight := 0
// for _, w := range weights {
// totalWeight += w
// }
// if totalWeight == 0 {
// return rand.Intn(len(weights))
// }
//
// rnd := rand.Intn(totalWeight)
//
// currentWeight := 0
// for i, w := range weights {
// currentWeight += w
// if rnd < currentWeight {
// return i
// }
// }
// return -1 // 如果权重都为 0,或者总权重为 0,则返回 -1
//}
//////////// struct field ptr 4 orm helpers ////////////
func Ptr[T any](arg T) *T {
return &arg
}
//////////// sql.Null* type helpers ////////////
func Null[T any](arg T) sql.Null[T] {
return sql.Null[T]{V: arg, Valid: true}
}