35 行
619 B
Go
35 行
619 B
Go
|
|
// Package modules - 共享工具函数
|
||
|
|
package modules
|
||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/rand"
|
||
|
|
"encoding/hex"
|
||
|
|
"strconv"
|
||
|
|
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
hhdb "github.com/honghuang-game/server/internal/db"
|
||
|
|
)
|
||
|
|
|
||
|
|
// hhdbPool 全局数据库连接池,供各模块使用
|
||
|
|
var hhdbPool *pgxpool.Pool
|
||
|
|
|
||
|
|
func init() {
|
||
|
|
hhdbPool = hhdb.Pool
|
||
|
|
}
|
||
|
|
|
||
|
|
// genUUID 生成唯一ID
|
||
|
|
func genUUID() string {
|
||
|
|
b := make([]byte, 16)
|
||
|
|
_, _ = rand.Read(b)
|
||
|
|
return hex.EncodeToString(b)
|
||
|
|
}
|
||
|
|
|
||
|
|
// parseFloat 安全解析浮点数
|
||
|
|
func parseFloat(s string, def float64) float64 {
|
||
|
|
f, err := strconv.ParseFloat(s, 64)
|
||
|
|
if err != nil {
|
||
|
|
return def
|
||
|
|
}
|
||
|
|
return f
|
||
|
|
}
|