answer/pkg/uid/id.go

41 lines
644 B
Go
Raw Normal View History

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() {
// 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()
}