sid.go 523 B

12345678910111213141516171819202122232425262728
  1. package sid
  2. import (
  3. "github.com/go-nunu/nunu-layout-advanced/pkg/helper/convert"
  4. "github.com/sony/sonyflake"
  5. )
  6. type Sid struct {
  7. sf *sonyflake.Sonyflake
  8. }
  9. func NewSid() *Sid {
  10. sf := sonyflake.NewSonyflake(sonyflake.Settings{})
  11. if sf == nil {
  12. panic("sonyflake not created")
  13. }
  14. return &Sid{sf}
  15. }
  16. func (s Sid) GenString() (string, error) {
  17. id, err := s.sf.NextID()
  18. if err != nil {
  19. return "", err
  20. }
  21. return convert.IntToBase62(int(id)), nil
  22. }
  23. func (s Sid) GenUint64() (uint64, error) {
  24. return s.sf.NextID()
  25. }