2022-09-27 17:59:05 +08:00
|
|
|
package uid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/snowflake"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SnowFlakeID snowflake id
|
|
|
|
type SnowFlakeID struct {
|
|
|
|
*snowflake.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
var snowFlakeIDGenerator *SnowFlakeID
|
|
|
|
|
|
|
|
func init() {
|
2022-11-01 15:25:44 +08:00
|
|
|
// todo
|
2022-09-27 17:59:05 +08:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
node, err := snowflake.NewNode(int64(rand.Intn(1000)) + 1)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
snowFlakeIDGenerator = &SnowFlakeID{node}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ID() snowflake.ID {
|
|
|
|
id := snowFlakeIDGenerator.Generate()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDStr12() string {
|
|
|
|
id := snowFlakeIDGenerator.Generate()
|
|
|
|
return id.Base58()
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDStr() string {
|
|
|
|
id := snowFlakeIDGenerator.Generate()
|
|
|
|
return id.Base32()
|
|
|
|
}
|