PaymentCenter/app/utils/snowflake/snow_flake.go

28 lines
402 B
Go

package snowflake
import (
sf "github.com/go-pay/util/snowflake"
"sync"
)
var once sync.Once
var snowflake *sf.Node
func InitSnowflake(workerID int64) (err error) {
// 创建雪花ID生成器
if snowflake == nil {
once.Do(func() {
snowflake, err = sf.NewNode(workerID)
})
}
return err
}
func GetID() int64 {
if snowflake == nil {
return 0
}
return snowflake.Generate().Int64()
}