1 Star 0 Fork 0

Cruvie Kang / kk_go_kit

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
kk_id.go 1.28 KB
Copy Edit Raw Blame History
cruvie authored 2024-03-16 22:26 . update
package kk_id
import (
"github.com/bwmarrin/snowflake"
"log/slog"
"strconv"
"strings"
"unicode/utf8"
)
var node *snowflake.Node
func init() {
var err error
node, err = snowflake.NewNode(1)
if err != nil {
slog.Error("init snowflake node error", "err", err)
panic(nil)
}
}
func GenerateId() (id snowflake.ID) {
// Generate a snowflake ID.
id = node.Generate()
return id
}
func GenerateUint64Id() (id uint64) {
// Generate a snowflake ID.
id = uint64(node.Generate())
return id
}
func CheckIdValid[T uint64 | string](ids ...T) (isValid bool) {
check := func(id T) (isValid bool) {
var p any = id
switch v := p.(type) {
case uint64:
idStr := strconv.FormatUint(v, 10)
return 19 == len(idStr) && v != 0
case string:
validString := utf8.ValidString(v)
if !validString {
return false
}
// 检查长度是否为19
if utf8.RuneCountInString(v) != 19 {
return false
}
runes := []rune(v)
// 检查是否以 0 开头
if string(runes[0]) == "0" {
return false
}
// 检查是否只包含数字
for _, ch := range runes {
if !strings.Contains("0123456789", string(ch)) {
return false
}
}
return true
default:
return false
}
}
for _, id := range ids {
if !check(id) {
return false
}
}
return true
}
1
https://gitee.com/cruvie/kk_go_kit.git
git@gitee.com:cruvie/kk_go_kit.git
cruvie
kk_go_kit
kk_go_kit
2c2b435ab572

Search