🎨 增加 Go 后端
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package sid
|
||||
|
||||
import (
|
||||
"whois-go/pkg/helper/convert"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sony/sonyflake"
|
||||
)
|
||||
|
||||
type Sid struct {
|
||||
sf *sonyflake.Sonyflake
|
||||
}
|
||||
|
||||
func NewSid() *Sid {
|
||||
sf := sonyflake.NewSonyflake(sonyflake.Settings{})
|
||||
if sf == nil {
|
||||
panic("sonyflake not created")
|
||||
}
|
||||
return &Sid{sf}
|
||||
}
|
||||
func (s Sid) GenString() (string, error) {
|
||||
// 生成分布式ID
|
||||
id, err := s.sf.NextID()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to generate sonyflake ID")
|
||||
}
|
||||
// 将ID转换为字符串
|
||||
return convert.IntToBase62(int(id)), nil
|
||||
}
|
||||
func (s Sid) GenUint64() (uint64, error) {
|
||||
// 生成分布式ID
|
||||
return s.sf.NextID()
|
||||
}
|
||||
Reference in New Issue
Block a user